g++动态库静态库混合链接

今天编译一个程序时报错:

g++ -static -o echo.fcgi echo_adaptor.o echo.o -L/usr/local/lib/ -lfastcgipp -L/usr/lib/ -lboost_thread -pthread -lboost_system -lboost_date_time -L/home/chu/lib/ -lwebframework -L/usr/local/lib/ -lctemplate_nothreads -L/usr/lib64/mysql/ -lmysqlclient -L/lib64/ -lrt -Wl,-R/usr/local/lib/ -Wl,-R/usr/lib/ -Wl,-R/usr/local/lib/ 

/usr/bin/ld: cannot find -lboost_thread
/usr/bin/ld: cannot find -lboost_system
/usr/bin/ld: cannot find -lboost_date_time
/usr/bin/ld: cannot find -lmysqlclient
/usr/bin/ld: cannot find -lrt
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lpthread
/usr/bin/ld: cannot find -lc
collect2: 错误:ld 返回 1

 

找不到boost库,但是明明安装了的(但是只有动态so,没有静态.a库文件):

chu@:~/SCFF_proj/trunk/example/echo/websvc[06:25:35]$ ll /usr/lib/libboost_thread*
-rw-r--r--. 1 root root   524 6月  10 2014 /usr/lib/libboost_thread-mt.so
-rwxr-xr-x. 1 root root 84980 6月  10 2014 /usr/lib/libboost_thread-mt.so.1.53.0
lrwxrwxrwx. 1 root root    21 2月  10 02:04 /usr/lib/libboost_thread.so -> libboost_thread-mt.so

 

分析编译命令,发现编译目标是静态库(-static ),猜测可能是ld只去查找了.a静态库文件,而忽略了动态库文件。

 

解决办法:

明确指定哪些库文件动态链接、哪些库文件静态链接:

g++ -static  -o echo.fcgi echo_adaptor.o echo.o -Wl,-Bstatic -L/usr/local/lib/ -lfastcgipp -L/home/chu/lib/ -lwebframework -L/usr/local/lib/ -lctemplate_nothreads -Wl,-Bdynamic -L/usr/lib/ -lboost_thread -pthread -lboost_system -lboost_date_time -L/usr/lib64/mysql/ -lmysqlclient -L/lib64/ -lrt -Wl,-R/usr/local/lib/ -Wl,-R/usr/lib/ -Wl,-R/usr/local/lib/ 

 

果然搞定了。

 

posted @ 2015-07-27 18:30    阅读(3467)  评论(0编辑  收藏  举报