linux 编译动态链接库 so,避免运行时才发现函数未 ...
(2014-07-09 09:04:47)ldd
nm
更多来源:http://www.zgjx114.com
defined 解释(好像英文版的才完整,中文的man ld不完整 可以直接查看网页
https://sourceware.org/binutils/docs-2.24/ld/Options.html#Options
)。就是让你链接时用的一个版本的so,运行时加载用的另外一个版本的so,可能你的加载时的so里面有这个符号,所以就先让你找不到符号也编译通过了。如果是编译exe,这中链接时找不到定义的符号的就直接给你报错了。
so动态链接库就不会报错。其实这种特性应该是比较少用,最好在so链接是碰到这个未找到的符号也是报错的好。
这里有3个参数可以使用--undefined symbols
和
我们的目的主要是编译一个so动态链接库时,把自己object里面未定义的符号report出来就可以了,用--no-undefined和--unresolved-symbols=ignore-in-shared-libs应该可以的。
ld
gcc -shared -Wl,-soname,libb.so.1,--no-undefined -o libb.so.1.2
gcc -shared
-Wl,-soname,libb.so.1,--unresolved-symbols=ignore-in-shared-libs
---------------------------------------
--no-undefined-z defsReport unresolved symbol references from
regular object files. This is done even if the linker is creating a
non-symbolic shared library. The
switch
----------------------------
--unresolved-symbols=method
Determine how to handle unresolved symbols. There are four
possible values for
`method':`ignore-all'Do not report
any unresolved symbols.
`report-all'Report all unresolved symbols. This is
the default.
`ignore-in-object-files'Report unresolved symbols
that are contained in shared libraries, but ignore them if they
come from regular object files.
`ignore-in-shared-libs'Report unresolved symbols
that come from regular object files, but ignore them if they come
from shared libraries. This can be useful when creating a dynamic
binary and it is known that all the shared libraries that it should
be referencing are included on the linker's command
line.
The behaviour for shared libraries on
their own can also be controlled by
the
Normally the linker will generate an
error message for each reported unresolved symbol but the
option
-----------------------------
--allow-shlib-undefined
--no-allow-shlib-undefinedAllows or disallows undefined symbols
in shared libraries. This switch is similar
to
The default behaviour is to report errors for any undefined symbols referenced in shared libraries if the linker is being used to create an executable, but to allow them if the linker is being used to create a shared library.
The reasons for allowing undefined symbol references in shared libraries specified at link time are that:
-
A shared library specified at link time may not be the same as the one that is available at load time, so the symbol might actually be resolvable at load time.
-
There are some operating systems, eg BeOS and HPPA, where undefined symbols in shared libraries are normal.
The BeOS kernel for example patches shared libraries at load time to select whichever function is most appropriate for the current architecture. This is used, for example, to dynamically select an appropriate memset function.

加载中…