ld时静态库的顺序

来源:互联网 发布:美工钢笔书法作品欣赏 编辑:程序博客网 时间:2024/04/27 15:31

android4.4中chromium的so在编译时将各部分都单独编译成单独的静态库,ld时将所有的静态库链接为so文件

ld时出现如下错误

external/chromium_org/components/autofill/core/browser/form_structure.cc:524: error: undefined reference to XmlElement::~XmlElement()'
很奇怪的错误

原因就是链接多个静态库,且这些静态库之间有依赖时,静态库在command line的顺序不对

举例:假设liba.a 依赖于libb.b,目标test需要liba和libb,那么生成test的命令应该为

gcc -o test -la -lb 而不是gcc -o test -lb -la

也可以使用ld的 --start-group xxx --end-group来将所有相互依赖的静态库放到xxx位置,只有就能保证search多次了


man ld

The linker will search an archive only once, at the location where it is specified on the command line.  If the archive defines a symbol whichwas undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive.  However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again. See the -( option for a way to force the linker to search archives multiple times.


-( archives -)--start-group archives --end-group    The archives should be a list of archive files.  They may be either explicit file names, or -l options.    The specified archives are searched repeatedly until no new undefined references are created.  Normally, an archive is searched only once in the order that it is specified on the command line.  If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the linker would not be able to resolve that reference.  By grouping the archives, they all be searched repeatedly until all possible references are resolved.    Using this option has a significant performance cost.  It is best to use it only when there are unavoidable circular references between two or more archives.


链接错误:

        有时候链接时,明明已经将所有的依赖库都加进去了,还是莫名其妙的报ld error,原因可能就是

        1. 上面说的依赖库是静态库时,有先后顺序,如果加上--start-group和--end-group就可以了。

        2. 还有可能是,你编译出来的库和你使用的库的版本不一样,导致接口有可能不用,即你使用的库的头文件和你编译依赖库的头文件不一样。不要想当然的认为一样,最好用用beyondcompare等工具比较一下





参考

http://stackoverflow.com/questions/4191275/libjingles-xmpppump-compilation-problem

0 0
原创粉丝点击