嵌入式开发之makefile---交叉编译静态库和动态库的生成和调用

c和cpp 混合的动态库生成:

$(LIBSO): $(COBJS) $(CPPOBJS) 
$(CPP) -shared -o $@ $^ $(LIBS)

//////////////////////////////////////////////////////////////////////////////

#for multchannel decoder makefile by pkf
BIN = client_system

BASE_INSTALL_DIR := /opt/arm-2009q1

BUILD_TOOL_DIR := $(BASE_INSTALL_DIR)

BUILD_TOOL_PREFIX := $(BUILD_TOOL_DIR)/bin/arm-none-linux-gnueabi-

CC = $(BUILD_TOOL_PREFIX)gcc

CPP = $(BUILD_TOOL_PREFIX)c++

LIBSO = librtspclient.so

INCS =

LIBS = -lpthread

SUBDIRS =

DEFINC = -I"./BasicUsageEnvironment/include" -I"./groupsock/include" -I"./liveMedia/include" \

-I"./UsageEnvironment/include"

#-I"./myclient"

INC_DIR := UsageEnvironment BasicUsageEnvironment liveMedia groupsock

INC := $(patsubst %,-I%/include,$(INC_DIR))

#CFLAGS := $(INC) -Wall -I $(KDIR)/include -DBSD=1 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64 -lpthread -I./ -g3 -O3

CSRCS = $(wildcard ./*.c ./BasicUsageEnvironment/*.c ./groupsock/*.c ./liveMedia/*.c \

./UsageEnvironment/*.c ./myclient/*.c)

CPPSRCS = $(wildcard ./*.cpp ./BasicUsageEnvironment/*.cpp ./groupsock/*.cpp ./liveMedia/*.cpp \

./UsageEnvironment/*.cpp ./myclient/*.cpp)

COBJS := $(CSRCS:.c=.o)

CPPOBJS := $(CPPSRCS:.cpp=.o)

MAKEDEPEND = gcc -MM -MT

CFLAGS = -O2 -Wall -DBSD=1 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64

CPPFLAGS = -O2 -Wall -DBSD=1 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64

CDEF = $(CSRCS:.c=.d)

CPPDEF = $(CPPSRCS:.cpp=.d)

PLATS = win32-debug win32-release linux-debug linux-release

none:

@echo "Please choose a platform"

@echo $(PLATS)

win32-debug:

$(MAKE) all INCS=-I"c:/mingw/include" LIBS="-L"c:/mingw/lib" -L"./../../lib/lxnet" -llxnet -lws2_32 -L"./../../lib/tinyxml" -ltinyxml" CFLAGS="-Wall -DWIN32 -DDEBUG -g" CPPFLAGS="-Wall -DWIN32 -DDEBUG -g"

win32-release:

$(MAKE) all INCS=-I"c:/mingw/include" LIBS="-L"c:/mingw/lib" -L"./../../lib/lxnet" -llxnet -lws2_32 -L"./../../lib/tinyxml" -ltinyxml" CFLAGS="-Wall -DWIN32 -DNDEBUG -O2" CPPFLAGS="-Wall -DWIN32 -DNDEBUG -O2"

linux-debug:

$(MAKE) all -lpthread CFLAGS="-Wall -DDEBUG -g" CPPFLAGS="-Wall -DDEBUG -g"

linux-release:

$(MAKE) all -lpthread CFLAGS="-Wall -DNDEBUG -O2" CPPFLAGS="-Wall -DNDEBUG -O2 -DBSD=1 -DSOCKLEN_T=socklen_t -D_LARGEFILE_SOURCE=1 -D_FILE_OFFSET_BITS=64"

all:$(BIN) fsupdate

lib:$(LIBSO) fsupdate

$(CDEF) : %.d : %.c

$(MAKEDEPEND) $(<:.c=.o) $< $(DEFINC) > $@

$(CPPDEF) : %.d : %.cpp

$(MAKEDEPEND) $(<:.cpp=.o) $< $(DEFINC) > $@

depend:

-rm $(CDEF)

-rm $(CPPDEF)

$(MAKE) $(CDEF)

$(MAKE) $(CPPDEF)

$(COBJS) : %.o: %.c

$(CC) -c $< -o $@ $(INCS) $(DEFINC) $(CFLAGS)

$(CPPOBJS) : %.o: %.cpp

$(CPP) -c $< -o $@ $(INCS) $(DEFINC) $(CPPFLAGS)

$(BIN) : $(COBJS) $(CPPOBJS)

$(CPP) -o $(BIN) $(COBJS) $(CPPOBJS) $(LIBS)

-rm $(COBJS) $(CPPOBJS)

$(LIBSO): $(COBJS) $(CPPOBJS)

$(CPP) -shared -o $@ $^ $(LIBS)

fsupdate:

# cp $(LIBSO) ../../../tirvideo_sdk/bell_lyb/rdk/demos/link_api_test_demos/linkapi

cp $(LIBSO) /home/tirvideo/lyb/armfs/opt/dvr_rdk/ti814x

# cp $(BIN) /home/tirvideo/lyb/armfs/opt/dvr_rdk/ti814x

.PHONY:clean cleanall

clean:

-rm $(BIN) $(COBJS) $(CPPOBJS) $(LIBSO)

-rm *.d

-rm *.obj

-rm *.o

-rm *.dep

-rm *.dep *.obj *.d

静态库的生成:

$(TargetLib): $(Cobjects)
$(AR) -rcs $(TargetLib) $^ 
# ar rcs ruv $(TargetLib) $^

/////////////////////////////////////////////////////////
Target = TestIni
TargetLib = inilib.a

BASE_INSTALL_DIR := /opt/arm-2009q1
BUILD_TOOL_DIR := $(BASE_INSTALL_DIR)
BUILD_TOOL_PREFIX := $(BUILD_TOOL_DIR)/bin/arm-none-linux-gnueabi-
AR := $(BUILD_TOOL_DIR)/bin/arm-none-linux-gnueabi-ar

GCC = $(BUILD_TOOL_PREFIX)gcc
G++ = $(BUILD_TOOL_PREFIX)g++

CInc = $(wildcard *.h)
CSrc = $(wildcard *.c)

Cobjects =$(CSrc:%.c=%.o)

$(Cobjects) : %.o: %.c

$(GCC) -c $< -o $@
#$(INCS) $(DEFINC) $(CFLAGS)
#Cobjects =$(patsubst %.c,%.o,$(CSrc))

lib:$(TargetLib)

release:$(Target)

$(Target): $(Cobjects)
echo $(GCC)
$(GCC) -o $@ $^

$(TargetLib): $(Cobjects)
$(AR) -rcs $(TargetLib) $^
# ar rcs ruv $(TargetLib) $^

##################test######################

#CInc += iniparser.h dictionary.h
#CSrc += iniparser.c dictionary.c
#Cobjects += iniparser.o dictionary.o
#CppObejects =
#$(Cobjects):$(Csrc)
# $(GCC) $(CInc) -c $<

#$(Target): iniparser.o dictionary.o
# $(GCC) -o $@ $^

#iniparser.o: iniparser.c
# $(GCC) $(CInc) -c $<
#dictionary.o: dictionary.c
# $(GCC) $(CInc) -c $<

clean:
rm -rf *.o $(Target) *.gch

值得注意的是在ubuntu上位机的编译

Cobjects =$(CSrc:%.c=%.o)

这一句就可以了,系统默认是cc编译即gcc

但是如果放在嵌入式下位机用的话就得加上下面这句话

$(Cobjects) : %.o: %.c 
$(GCC) -c $< -o $@

静态库的调用:

#for makefile web module by pkf
.SUFFIXES:
.SUFFIXES: .o .c
.PHONY: clean mrclean distclean depend all dist

INSTALLDIR = $(EXEC_DIR)

GCC_FLAGS = -Wstrict-prototypes -Wpointer-arith -Wcast-align -Wcast-qual\
-Wtraditional\
-Wshadow\
-Wconversion\
-Waggregate-return\
-Wmissing-prototypes\
-Wnested-externs\
-Wall \
-Wundef -Wwrite-strings -Wredundant-decls -Winline \
-mshort-calls

BASE_INSTALL_DIR := /opt/arm-2009q1
BUILD_TOOL_DIR := $(BASE_INSTALL_DIR)
BUILD_TOOL_PREFIX := $(BUILD_TOOL_DIR)/bin/arm-none-linux-gnueabi-

srcdir = .

LDFLAGS = -g -lpthread -ldl
LIBS =

CFLAGS = -O3 -pipe -Wall

CFLAGS += -I./cgi.h

LEX = flex
CC = $(BUILD_TOOL_PREFIX)gcc
CPP = $(BUILD_TOOL_PREFIX)gcc -E

SOURCES = data_process.c data_update.c server_api.c net_config.c
USOURCES = upload.c cgic.c

OBJS =$(SOURCES:.c=.o)
UOBJS =$(USOURCES:.c=.o)

all: data_update.cgi upload.cgi fsupdate

install: data_update.cgi
install data_update.cgi $(INSTALLDIR)
# make fsupdate

data_update.cgi: $(OBJS) $(LIBS)
$(CC) -o $@ $^ -L. inilib.a $(LDFLAGS) $(CFLAGS)

upload.cgi: $(UOBJS) $(LIBS)
$(CC) -o $@ $^ $(LDFLAGS) $(CFLAGS)

clean:
rm -f $(OBJS) $(UOBJS) data_update.cgi upload.cgi

fsupdate:
@echo "--------------------------------------------------------------"
@echo "| !!!!!!!!!!!!!!<file update sucess>!!!!!!!!!!!!! |"
@echo "--------------------------------------------------------------"

cp *.html /home/tirvideo/lyb/armfs/var/www/
cp *.txt /home/tirvideo/lyb/armfs/var/www/
cp ./*.cgi /home/tirvideo/lyb/armfs/var/www/cgi-bin/
chmod 777 /home/tirvideo/lyb/armfs/var/www/*.html
chmod 777 /home/tirvideo/lyb/armfs/var/www/cgi-bin/*.cgi

distclean: mrclean

mrclean: clean
rm -f config.status Makefile config.log

.depend:
$(CPP) -MM $(SOURCES) > .depend

depend:
-rm -f .depend
$(MAKE) .depend

include .depend

# tags
tags: $(SOURCES)
ctags -o tags $^ *.h

# dist
dist:
$(MAKE) clean
./makedist.sh

# object dump
boa.objdump: data_update.cgi
objdump --disassemble-all --source data_update.cgi > $@

http://www.cnblogs.com/zhoujg/p/5054122.html

上一篇:Linux编程(3) MakeFile


下一篇:【设置】Nginx配置文件具体配置解释