1

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

我可以知道使用的是静态的还是动态的?

4

2 回答 2

3

ld我的 Linux(Ubuntu 12.10 机器)上的GNU将.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.
于 2013-05-28T20:26:40.127 回答
2

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

如果要强制它使用静态库,可以使用-static 链接选项

于 2013-05-28T20:25:09.963 回答