one makefile file

#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

 

from:http://blog.csdn.net/gdutliuyun827/article/details/17320599

posted on 2015-10-29 11:32  浪子回头jin不换  阅读(203)  评论(0编辑  收藏  举报

导航