g++ 静态库连接顺序的巨坑

在编译最新版本(12.04)的alljoyn的chat示例的时候,想使用bundle daemon,依照在以前的经验修改文件:alljoyn-14.02.00-src/build/linux/x86_64/debug/dist/samples/chat/makefile:

LIBS = -lalljoyn ../../lib/BundledRouter.o -lajrouter -lstdc++ -lcrypto -lpthread -lrt

 

 

1). 编译ok,但运行时报错:

./chat

./chat: error while loading shared libraries: liballjoyn.so: cannot open shared object file: No such file or directory

 

原来../../lib/目录下有两个库:

ll ../../lib/

total 71552

drwxr-xr-x 2 z.rao CAF     4096 Apr  9 19:56 ./

drwxr-xr-x 6 z.rao CAF     4096 Apr  9 18:35 ../

-rw-r--r-- 1 z.rao CAF   529336 Apr  9 18:36 BundledRouter.o

-rw-r--r-- 1 z.rao CAF 38765950 Apr  9 18:36 libajrouter.a

-rw-r--r-- 1 z.rao CAF 24335844 Apr  9 18:35 liballjoyn.a

-rwxr-xr-x 1 z.rao CAF  9621229 Apr  9 19:56 liballjoyn.so*

默认连接的是动态库:liballjoyn.so*

虽然可以通过ldconfig搞定动态库的连接问题,但考虑到程序的发布问题,我决定连接静态库liballjoyn.a

 

2).于是,删除liballjoyn.so,这样程序就会连接到静态库liballjoyn.a了:

rm ../../lib/liballjoyn.so

make clean;make

很不幸报错:

g++ -c -Wall -pipe -std=c++98 -fno-rtti -fno-exceptions -Wno-long-long -Wno-deprecated -g -DQCC_OS_LINUX -DQCC_OS_GROUP_POSIX -DQCC_CPU_X86 -I../../inc -o chat.o chat.cc

g++ -o chat chat.o -L../../lib -lalljoyn ../../lib/BundledRouter.o -lajrouter -lstdc++ -lcrypto -lpthread -lrt

../../lib/BundledRouter.o: In function `BundledRouter::Start(ajn::NullTransport*)':

/home/z.rao/work/Library/alljoyn-14.02.00-src/alljoyn_core/router/bundled/BundledRouter.cc:263: undefined reference to `qcc::LoggerSetting::GetLoggerSetting(char const*, int, bool, _IO_FILE*)'

../../lib/BundledRouter.o: In function `ajn::PasswordManager::GetPassword()':

/home/z.rao/work/Library/alljoyn-14.02.00-src/alljoyn_core/inc/alljoyn/PasswordManager.h:65: undefined reference to `ajn::PasswordManager::password'

../../lib/BundledRouter.o: In function `ajn::PasswordManager::GetAuthMechanism()':

/home/z.rao/work/Library/alljoyn-14.02.00-src/alljoyn_core/inc/alljoyn/PasswordManager.h:71: undefined reference to `ajn::PasswordManager::authMechanism'

../../lib/libajrouter.a(TCPTransport.o): In function `ajn::TCPTransport::GetListenAddresses(ajn::SessionOpts const&, std::vector<qcc::String, std::allocator<qcc::String> >&) const':

/home/z.rao/work/Library/alljoyn-14.02.00-src/alljoyn_core/router/TCPTransport.cc:1193: undefined reference to `qcc::IfConfig(std::vector<qcc::IfConfigEntry, std::allocator<qcc::IfConfigEntry> >&)'

../../lib/libajrouter.a(TCPTransport.o): In function `ajn::TCPTransport::Connect(char const*, ajn::SessionOpts const&, qcc::ManagedObj<ajn::_BusEndpoint>&)':

没有定义?

 

看看导出了哪些符号?

nm -C ../../lib/liballjoyn.a |grep GetLoggerSetting

0000000000000476 T qcc::LoggerSetting::GetLoggerSetting(char const*, int, bool, _IO_FILE*)

0000000000000000 W qcc::LoggerSetting::GetLoggerSetting()

明明已经定义了啊.....哭。。。

之前连接动态库liballjoyn.so就可以,静态库liballjoyn.a为什么不行呢?。。。。百思不得其解

 

3).想来想去,可能是静态库的连接顺序导致了循环依赖,除此以外似乎没有其他可能了。

于是网上猛搜一通,发现

http://www.cnblogs.com/wujianlundao/archive/2012/06/06/2538125.html

 

将makefile修改为如下形式:

LIBS = -Xlinker "-(" -lalljoyn ../../lib/BundledRouter.o -lajrouter -Xlinker "-)" -lstdc++ -lcrypto -lpthread -lrt

果然搞定了!

 

要问-Xlinker干啥的?。。。。哈哈啊

 

 

 

 

 

 

 

你可能感兴趣的:(静态)