当前位置: 技术问答>linux和unix
makefile中调用共享库顺序的问题
来源: 互联网 发布时间:2017-05-25
本文导语: 问题的提出: 最近在Linux下操作xml时,用到网上很流行的tinyxml库,但为了操作的方便,编写了一个库对tinyxml库进行了封装,且起名为dansirxml。dansirxml共享库很简单,就一个CDanSirXml类,分为dansirxml.cpp和dansirx...
问题的提出:
最近在Linux下操作xml时,用到网上很流行的tinyxml库,但为了操作的方便,编写了一个库对tinyxml库进行了封装,且起名为dansirxml。dansirxml共享库很简单,就一个CDanSirXml类,分为dansirxml.cpp和dansirxml.h两个文件。自己编写makefile文件生成
libdansirxml.so共享库了。由于libdansirxml.so共享库是对libtinyxml.so库的封装,即libdansirxml.so共享库依赖于libtinyxml.so库的。在主程序的makefile文件中调用libdansirxml.so共享库,如下:
MYLIBS = -L../../../../OldProj/cs/mptmsu/lib -lsqlite3 -llog4cplus -ltinyxml -ldansirxml
即将libtinyxml.so共享库放在libdansirxml.so的前面,则make主程序时,会出现找不到libtinyxml.so库中的很多东东的连接错误,但如果将libdansirxml.so和libtinyxml交换下顺序,即将上面的一行改成如下所示:
MYLIBS = -L../../../../OldProj/cs/mptmsu/lib -lsqlite3 -llog4cplus -ldansirxml -ltinyxml
则make主程序时不会出错,这是为何呢?在主程序中调用外部共享库时有顺序要求吗?如果有的话,顺序的规则是咋样的呢?
最近在Linux下操作xml时,用到网上很流行的tinyxml库,但为了操作的方便,编写了一个库对tinyxml库进行了封装,且起名为dansirxml。dansirxml共享库很简单,就一个CDanSirXml类,分为dansirxml.cpp和dansirxml.h两个文件。自己编写makefile文件生成
libdansirxml.so共享库了。由于libdansirxml.so共享库是对libtinyxml.so库的封装,即libdansirxml.so共享库依赖于libtinyxml.so库的。在主程序的makefile文件中调用libdansirxml.so共享库,如下:
MYLIBS = -L../../../../OldProj/cs/mptmsu/lib -lsqlite3 -llog4cplus -ltinyxml -ldansirxml
即将libtinyxml.so共享库放在libdansirxml.so的前面,则make主程序时,会出现找不到libtinyxml.so库中的很多东东的连接错误,但如果将libdansirxml.so和libtinyxml交换下顺序,即将上面的一行改成如下所示:
MYLIBS = -L../../../../OldProj/cs/mptmsu/lib -lsqlite3 -llog4cplus -ldansirxml -ltinyxml
则make主程序时不会出错,这是为何呢?在主程序中调用外部共享库时有顺序要求吗?如果有的话,顺序的规则是咋样的呢?
|
gcc手册:
-llibrary
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
-llibrary
-l library
Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.
|
Classic question...