cc 静态链接:如何导入一个 .a 库的所有符号?
我有 2 个静态库,
libalgha.a
,有 2 个函数:func1()
和 func2()
和
libbeta.a 具有 2 个函数:
func3()
和 func4()
我有 1 个可执行文件 (mytest
) 与这 2 个库链接。可执行文件仅调用 func1()
和 func3()
。因此,据我了解,链接器只会将这两个函数的符号放入可执行文件中。
可执行文件在运行时使用 dlopen()
加载共享库 (libgamma.so
),并且该共享库使用来自的函数 func2()
libalgha.a。运行 mytest
时出现错误:
符号 func2:找不到引用的符号
由于 Solaris 32/64 位问题,我无法将 libalgha.a
包含在 libgamma.so
中。
那么如何将 mytest
与 libalgha.a
的所有符号链接起来?
I have 2 static libraries,
libalgha.a
with 2 functions: func1()
and func2()
and
libbeta.a
with 2 functions: func3()
and func4()
I have 1 executable (mytest
) linked with these 2 libs. The executable calls only func1()
and func3()
. So as I understand the linker will put only the symbol of these 2 functions into the executable.
The executable is loading with dlopen()
in runtime a shared library (libgamma.so
) and this shared library uses the function func2()
from libalgha.a
. When running mytest
I get the error:
symbol func2: referenced symbol not found
I can't include libalgha.a
inside libgamma.so
because of a Solaris 32/64 bits issue.
So how can I link mytest
with all symbols of libalgha.a
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在程序源中包含对
func2
的引用,例如:或者,使用
ld -r
将整个库链接到一个对象,然后将该对象链接到可执行文件,而不是图书馆。您可能需要也可能不需要提取对象,例如,如果以下方法不起作用:则执行
Include a reference to
func2
in your program source like:Alternatively, link the whole library into one object, using
ld -r
, and link that object to the executable, instead of the library. You may or may not need to extract objects, e.g., if the following does not work:then do
也许
-u func2
会有所帮助。请参阅手册页。不幸的是,GNU 上似乎没有
--whole-archive
选项。Maybe
-u func2
would help. See the manual page.Unfortunately, there doesn't seem to be a
--whole-archive
option as on GNU.