linux - linux 包管理器安装的库是静态链接还是动态链接?

标签 linux cmake shared-libraries static-libraries package-management

如果cryptopp例如使用 sudo apt install libcrypto++-dev 安装然后包括使用 #include <cryptopp/base64.h> , 这个库是静态链接还是动态链接?

CMakeLists.txt 包括 cryptopptarget_link_libraries .

最佳答案

will [a library installed via a package manager] be statically or dynamically linked?

这取决于几个因素。首先,两个库都必须可用。对于 Unix 和 Linux 上的 Crypto++,静态库和动态库都可用。在 Windows 上,仅提供静态库。

其次,假设两个库都可用,链接器的配置很重要。在 Linux 上使用 ld , 动态库总是默认使用。在 OS X 上,默认情况下也始终使用动态库。在 Windows 上,链接器配置不会影响因素,因为选项控制它。

第三,它取决于链接器选项。在 Windows 上 - 如果动态库可用 - 这将取决于您链接到的库。它可以是动态链接库的导入库上的静态。在 Linux 上使用 ld您可以使用 :filename 链接到静态库:

-l namespec

--library=namespec

Add the archive or object file specified by namespec to the list of files to link. This option may be used any number of times. If namespec is of the form :filename, ld will search the library path for a file called filename, otherwise it will search the library path for a file called libnamespec.a.

On systems which support shared libraries, ld may also search for files other than libnamespec.a. Specifically, on ELF and SunOS systems, ld will search a directory for a library called libnamespec.so before searching for one called libnamespec.a. (By convention, a .so extension indicates a shared library.) Note that this behavior does not apply to :filename, which always specifies a file called filename.

The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again.

最后,使用 CMake 时,行为并不是一件简单的事情。默认行为可能是不添加任何内容。将 -lcryptopp-l:cryptopp 添加到您的 LDFLAGSLDLIBS 将无效,因为 CMake 不接受习惯旗帜。您必须手动将库添加到每个目标。

关于linux - linux 包管理器安装的库是静态链接还是动态链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46508524/

相关文章:

c - 重定向时强制程序刷新其标准输出

c++ - 如何使用 cmake 为嵌套子目录设置 Visual Studio 过滤器

python - Raspbian Stretch 上的 CMake 安装问题

c++ - 从共享库导出模板化 C++ 类在 Linux 上如何工作?

Linux更改组权限以匹配所有者权限

Linux 排序 - 求助

java - FTP Get 适用于 Windows,但不适用于 Linux

c++ - 使用 CMAKE 和 RPATH 捆绑 FFMPEG

linux - Linux下共享库加载地址

macos - 在 OS X 上以编程方式查找共享库中本地符号的偏移量