cc 静态链接:如何导入一个 .a 库的所有符号?

发布于 2024-12-07 06:28:01 字数 712 浏览 5 评论 0原文

我有 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 中。

那么如何将 mytestlibalgha.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

九八野马 2024-12-14 06:28:01

在程序源中包含对 func2 的引用,例如:

extern void func2 ();
void (*pfunc2)() = func2;

或者,使用 ld -r 将整个库链接到一个对象,然后将该对象链接到可执行文件,而不是图书馆。您可能需要也可能不需要提取对象,例如,如果以下方法不起作用:

ld -r -o libbeta.o libbeta.a

则执行

mkdir x
cd x
ar x ../libbeta.a
ld -r -o ../libbeta.o *.o
cd ..
rm -rf x

Include a reference to func2 in your program source like:

extern void func2 ();
void (*pfunc2)() = func2;

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:

ld -r -o libbeta.o libbeta.a

then do

mkdir x
cd x
ar x ../libbeta.a
ld -r -o ../libbeta.o *.o
cd ..
rm -rf x
爱殇璃 2024-12-14 06:28:01

也许 -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文