在编译时链接共享对象

时间:2022-09-18 12:26:37

In Windows, many .dlls come with a static .lib counterpart. My understanding is that the .lib counterpart basically contains LoadProcAddress calls so that the programmer doesn't have to do it him/herself. Essentially, a time saver. When I switched to Linux, I was assuming the situation was the same, replacing .dll with .so and .lib with .a, but I have come to a situation that is showing me this is wrong and I can't figure out what is going on:

在Windows中,许多.dll都带有静态的.lib对应物。我的理解是.lib对应物基本上包含LoadProcAddress调用,这样程序员就不必自己动手了。基本上,节省时间。当我切换到Linux时,我假设情况是一样的,用.a替换.dll和.lib的.lib,但是我遇到的情况是告诉我这是错的,我无法弄清楚是什么正在进行:

I am using a library that comes as a .a/.so pair. I was linking against the .a, but when I executed ldd on the binary that was produced, it contained no reference to the corresponding .so file. So then, I tried linking against the .so file and to my surprise, this worked. In addition, the .so file showed up when I executed ldd against the resulting binary.

我使用的是一个.a / .so对的库。我正在链接.a,但是当我在生成的二进制文件上执行ldd时,它不包含对相应.so文件的引用。那么,我尝试链接.so文件,令我惊讶的是,这很有效。另外,当我对生成的二进制文件执行ldd时,.so文件出现了。

So, I am really confused as to what is going on. In Windows, I would never think to link against a .dll file. Also, in Windows, if a .dll file was accompanied with a .lib and I linked against the .lib at compile-time, then I would expect to have a dependency on the corresponding .dll at runtime. Both these things are not true in this case.

所以,我对发生的事情感到很困惑。在Windows中,我永远不会想到链接.dll文件。此外,在Windows中,如果.dll文件附带.lib并且我在编译时链接到.lib,那么我希望在运行时依赖于相应的.dll。在这种情况下,这两件事都不正确。

Yes, I have read the basic tutorials about shared objects in Linux, but everything I read seems to indicate that my initial assumption was correct. By the way, I should mention that I am using Code::Blocks as an IDE, which I know complicates things, but I am 99% sure that when I tell it to link against the .so file, it is not simply swapping out the .a file because the resulting binary is smaller. (Plus the whole business about ldd...)

是的,我已经阅读了有关Linux*享对象的基本教程,但我读到的所有内容似乎表明我的初始假设是正确的。顺便说一句,我应该提到我使用Code :: Blocks作为IDE,我知道这会让事情变得复杂,但是当我告诉它链接到.so文件时,我肯定99%,它不仅仅是交换掉.a文件,因为生成的二进制文件较小。 (加上关于ldd的整个业务...)

Anyway, thanks in advance.

无论如何,提前谢谢。

3 个解决方案

#1


1  

I was linking against the .a, but when I executed ldd on the binary that was produced, it contained no reference to the corresponding .so file.

我正在链接.a,但是当我在生成的二进制文件上执行ldd时,它不包含对相应.so文件的引用。

This is expected. When you link statically, the static library's code is integrated into the resulting binary. There are no more references to or dependencies on the static library.

这是预料之中的。静态链接时,静态库的代码将集成到生成的二进制文件中。静态库没有更多的引用或依赖。

So then, I tried linking against the .so file and to my surprise, this worked.

那么,我尝试链接.so文件,令我惊讶的是,这很有效。

What do you mean, that the static linking did not work? There's no reason that it shouldn't...

你是什​​么意思,静态链接不起作用?它没有理由不......

#2


1  

.lib are used in Windows to dynamically link. You don't have them in Linux, you link with .so directly. The .a file is the statically built library, you use it to link statically.

.lib在Windows中用于动态链接。您没有在Linux中使用它们,而是直接与.so链接。 .a文件是静态构建的库,您可以使用它静态链接。

#3


0  

To add to already correct answer by tharibo - in some situations (e.g. delayed shared library load) it might be desirable to do it the Windows way i.e. by linking against a static stub instead of .so. Such stubs can be written by hand, generated by project-specific scripts or by generic Implib.so tool.

要通过tharibo添加已经正确的答案 - 在某些情况下(例如延迟的共享库加载),可能需要以Windows方式执行,即通过链接静态存根而不是.so。这些存根可以手工编写,由项目特定的脚本或通用的Implib.so工具生成。

#1


1  

I was linking against the .a, but when I executed ldd on the binary that was produced, it contained no reference to the corresponding .so file.

我正在链接.a,但是当我在生成的二进制文件上执行ldd时,它不包含对相应.so文件的引用。

This is expected. When you link statically, the static library's code is integrated into the resulting binary. There are no more references to or dependencies on the static library.

这是预料之中的。静态链接时,静态库的代码将集成到生成的二进制文件中。静态库没有更多的引用或依赖。

So then, I tried linking against the .so file and to my surprise, this worked.

那么,我尝试链接.so文件,令我惊讶的是,这很有效。

What do you mean, that the static linking did not work? There's no reason that it shouldn't...

你是什​​么意思,静态链接不起作用?它没有理由不......

#2


1  

.lib are used in Windows to dynamically link. You don't have them in Linux, you link with .so directly. The .a file is the statically built library, you use it to link statically.

.lib在Windows中用于动态链接。您没有在Linux中使用它们,而是直接与.so链接。 .a文件是静态构建的库,您可以使用它静态链接。

#3


0  

To add to already correct answer by tharibo - in some situations (e.g. delayed shared library load) it might be desirable to do it the Windows way i.e. by linking against a static stub instead of .so. Such stubs can be written by hand, generated by project-specific scripts or by generic Implib.so tool.

要通过tharibo添加已经正确的答案 - 在某些情况下(例如延迟的共享库加载),可能需要以Windows方式执行,即通过链接静态存根而不是.so。这些存根可以手工编写,由项目特定的脚本或通用的Implib.so工具生成。