如果libfoo.a和libfoo.so都在库路径上,-lfoo会链接什么?

时间:2013-05-28 20:23:02

标签: gcc

使用gcc和gnu ld,我正在编译依赖于库的二进制文件(在这种情况下是一个共享对象),比方说libfoo。 libfoo同时作为.a和.so文件出现在同一目录中。

我可以知道正在使用哪种,静态还是动态?

2 个答案:

答案 0 :(得分:3)

我的Linux上的GNU ld(Ubuntu 12.10框)将首先加载.so文件。

来自man ld

   -l namespec
   --library=namespec
       Add the archive or object file specified by namespec to the list of
       files to link.  This option may be used any number of times.  If
       namespec is of the form :filename, ld will search the library path
       for a file called filename, otherwise it will search the library
       path for a file called libnamespec.a.

       On systems which support shared libraries, ld may also search for
       files other than libnamespec.a.  Specifically, on ELF and SunOS
       systems, ld will search a directory for a library called
       libnamespec.so before searching for one called libnamespec.a.  (By
       convention, a ".so" extension indicates a shared library.)  Note
       that this behavior does not apply to :filename, which always
       specifies a file called filename.

答案 1 :(得分:2)

gcc(和ld,它真正在幕后运行)默认为动态链接(如果可用)。如果需要,可以检查输出二进制文件以查看它的作用。

如果您想强制它使用静态库,您可以使用-static link option