Mac OS上的混合静态和动态链接 [英] Mixed static and dynamic link on Mac OS
问题描述
我想使用gcc生成一个共享库,但是我想链接一些静态依赖的其他库。现在生成我使用的标准动态链接输出文件。
gcc -dynamiclib * .o -lfoo -lbar -o outfile
这将是
gcc -shared * .o -lfoo -lbar -o outfile
on在Linux系统上的binutils ld
。现在,如果我想让libfoo和libbar静态链接,我可以直接命名静态库
$ $ $ $ $ $ $ $ gcc -dynamiclib * .o /但是,usr / lib / libfoo.a /usr/lib/libbar.a -o outfile
我必须自己去寻找库文件。 GNU binutils ld
支持:
gcc -shared * .o - l:libfoo.a -l:libbar.a -o outfile
但苹果的 ld
doesnt。
- 有没有办法让苹果的
ld
自己查找静态库? - 如果没有,是否有另一种避免命名档案确切位置的方法,例如使用
-static
开关从要求libfoo和libbar的目标文件中生成一个中间输出文件,并将该文件与剩余的目标文件一起链接以创建动态对象?
引用 QA1393 ,
通常,链接器遍历搜索路径中的每个路径一次找到一个动态版本的库。如果没有找到,它会遍历每个寻找相同库的静态版本的路径。如果两个库都位于相同的目录中,而没有使用-l链接器选项和每个库的绝对路径,则无法在相应的dylib上选择静态库。
按照QA1393的建议,您可以将静态库放置在不同的目录中,在其他目录之前使用 -L / path / to / static / libraries
可能指向动态库的 -L
出现,并且 -search_paths_first
,这样链接器就会同时尝试.dylib(它将不会在那里)和.a在搜索下一个搜索路径等之前的第一个搜索路径。
I want to use gcc to produce a shared library, but i want to link some other libraries it depends on statically. Now to produce the "standard" dynamically linked output file i use
gcc -dynamiclib *.o -lfoo -lbar -o outfile
which would be
gcc -shared *.o -lfoo -lbar -o outfile
on for a binutils ld
on a linux system. Now if i want libfoo and libbar to be linked statically, I can name the static libraries directly
gcc -dynamiclib *.o /usr/lib/libfoo.a /usr/lib/libbar.a -o outfile
however, that way i have to look for the library files myself. GNU binutils ld
supports this:
gcc -shared *.o -l:libfoo.a -l:libbar.a -o outfile
but apple's ld
doesnt.
- Is there a way to make apple's
ld
look for the static libraries himself? - If not, is there another way that would avoid naming the exact location of the archives, e.g. producing an intermediate output file out of the object files requiring libfoo and libbar with the
-static
switch and linking that file together with the remaining objectfiles to create the dynamic object?
Quoting QA1393,
Normally, the linker goes through each path in the search paths one at a time to find a dynamic version of the library. If none is found, it goes through each of those paths looking for a static version of the same library. There is no way to choose a static library over a corresponding dylib if both libraries are in the same directory without using the -l linker option and absolute paths to each library.
As recommended by QA1393, you can place your static libraries in a different directory, use -L/path/to/static/libraries
before other occurrences of -L
that could point to dynamic libraries, and -search_paths_first
so that the linker tries both .dylib (which won’t be there) and .a in the first search path before searching the next search path and so forth.
这篇关于Mac OS上的混合静态和动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!