如何强制链接器使用共享库而不是静态库?
这是Linux程式设计书的报价:
This is a quote from Linux programming book:
%gcc -o app app.o -L。 -ltest
假设 libtest.a
和 libtest.so
是可用的。然后链接器必须
选择一个库而不是其他库。链接器搜索每个目录(第一个
那些指定了 -L
选项,然后在标准目录中的那些)。当
链接器找到一个包含 libtest的目录。 a
或 libtest.so
,链接器将停止
搜索目录。如果目录中只存在两个变体中的一个,链接器
选择该变体。否则,链接器选择共享库版本,除非
明确指示它。否则,您可以使用 -static
选项来请求静态
存档。例如,以下行将使用 libtest.a
归档,即使 libtest.so
共享库也可用:
Suppose that both libtest.a
and libtest.so
are available.Then the linker must
choose one of the libraries and not the other.The linker searches each directory (first
those specified with -L
options, and then those in the standard directories).When the
linker finds a directory that contains either libtest.a
or libtest.so
, the linker stops
search directories. If only one of the two variants is present in the directory, the linker
chooses that variant. Otherwise, the linker chooses the shared library version, unless
you explicitly instruct it otherwise.You can use the -static
option to demand static
archives. For example, the following line will use the libtest.a
archive, even if the
libtest.so
shared library is also available:
%gcc -static -o app app.o -L。 -ltest
因为如果链接器遇到包含 libtest.a
它停止搜索并使用静态库,如何强制链接器只搜索共享库,而不是静态?
Since if the linker encounters the directory that contains libtest.a
it stops search and uses that static library, how to force the linker to search only for shared library, and not for static?
%gcc -o app app.o -L。 libtest.so
?
您可以使用 -l ld
didn'的旧版本)时,以 -l:filename
t)
You could use -l
option in its form -l:filename
if your linker supports it (older versions of ld
didn't)
gcc -o app app.o -L. -l:libtest.so
其他选项是直接使用文件名而不带 -l
和 -L
Other option is to use the filename directly without -l
and -L
gcc -o app app.o /path/to/library/libtest.so