發乙個自己用過的makefile

2021-06-19 17:05:41 字數 2666 閱讀 7668

#gcc test.cpp -l. -wl,-bdynamic -ltestlib -wl,-bstatic -ltestlib  -wl,-bdynamic 

#make clean; make init; make server_name=krc_search

#make clean; make init; make server_name=krc_feedback

cc=g++

obj_path=./objs

server_src = $(wildcard *.cpp)

server_obj = $(patsubst %.cpp, $(obj_path)/%.o, $(server_src))

incdir= -i/usr/local/freetds0.91/include \

-i/usr/local/include \

-i/usr/include \

-i../component/inc \

-i../component/inc/jsoncpp \

-i../redis-2.6.13/deps -i./

libdir= -l/usr/lib64 -l/usr/lib64/mysql -l/usr/local/freetds0.91/lib -l../component/inc

solibs= -lcrypto -lcurl -lmysqlclient -lsybdb

wlpath= -wl,--rpath=/usr/local/freetds0.91/lib \

-wl,--rpath=/usr/lib64/boost \

-wl,--rpath=/usr/local/lib \

-wl,--rpath=/usr/lib \

-wl,--rpath=/usr/lib64/mysql \

-wl,--rpath=/usr/local/freetds0.91/lib

arlibs= -wl,-bstatic ../component/inc/libcomponent.a \

-wl,-bstatic /usr/lib64/boost/libboost_system.a \

-wl,-bstatic /usr/lib64/boost/libboost_thread.a \

-wl,-bstatic /usr/lib64/boost/libboost_regex.a \

-wl,-bstatic /usr/local/lib/libevent.a \

-wl,-bstatic /usr/local/freetds0.91/lib/libsybdb.a \

-wl,-bstatic ../component/inc/jsoncpp/libjsoncpp.a \

-wl,-bstatic ../redis-2.6.13/deps/hiredis/libhiredis.a

$(server_name):$(server_obj)

$(cc) -wall -pthread -d krc_server_name=$(server_name)_flag -g -o $@ $(incdir) $(wlpath) $^ $(arlibs) $(libdir) -wl,-bdynamic $(solibs)

init:

@mkdir -p ./objs

unit_test: commonutil.cpp testutil/dgramsock.cpp testutil/testutil.cpp

$(cc) -pthread -g -o $@ $(incdir) $(wlpath) $^ $(arlibs) $(libdir) -wl,-bdynamic $(solibs)

clean:

rm -rf *.o krc_search unit_test krc_feedback objs

$(server_obj):$(obj_path)/%.o:%.cpp

$(cc) -c -g -d krc_server_name=$(server_name)_flag $(incdir) $< -o $@

help:

@echo "make clean; make init; make server_name=krc_search"

@echo "make clean; make init; make server_name=krc_feedback"

linux預設的鏈結是動態庫,找不到動態庫的情況下才會選擇靜態庫;

使用方式為:gcc test.cpp -l. -ltestlib

如果當前目錄有兩個庫libtestlib.so libtestlib.a 則肯定是連線libtestlib.so。如果要指定為連線靜態庫;

使用方式為:gcc test.cpp -l. -static -ltestlib

當對動態庫與靜態庫混合連線的時候,使用-static會導致所有的庫都使用靜態連線的方式。這時需要作用-wl的方式:

gcc test.cpp -l. -wl,-bstatic -ltestlib  -wl,-bdynamic -ltestlib

另外還要注意系統的執行庫使用動態連線的方式,所以當動態庫在靜態庫前面連線時,必須在命令列最後使用動態連線的命令才能正常連線,如:

gcc test.cpp -l. -wl,-bdynamic -ltestlib -wl,-bstatic -ltestlib  -wl,-bdynamic

Linux下書寫乙個簡單的makefile

在linux下我們經常需要 編譯 而談起編譯就離不開 makefile 了,簡單來說 makefile 是編譯的乙個引導檔案,關於它的詳細介紹網上有許多資料,這裡只為介紹如何書寫乙個簡單的 makefile sources include libs objects sources cpp o tar...

linux編譯單獨乙個模組makefile寫法

單個檔案編譯乙個模組 ifneq kernelrelease obj m mini2440 leds.o else kdir opt friendlyarm mini2440 linux 2.6.32.2 all make c kdir m pwd modules arch arm cross co...

常用的乙個簡單生成靜態庫的makefile

makefile linux for s21sdk sample testnc shell bin sh cc mipsel linux gcc ar mipsel linux ar ccsipdir shell cd pwd cflag o2 w wall incpath i.include i....