shared-libraries - 在 pkg-config 中使用 Requires 与 Requires.private 时,链接过程有何不同?

标签 shared-libraries pkg-config

我指的是这个 guide to pkg-config用于学习如何编写。

在一处,它提到了以下有关 RequiresRequires.private 字段的内容。

Requires and Requires.private define other modules needed by the library. It is usually preferred to use the private variant of Requires to avoid exposing unnecessary libraries to the program that is linking with your library. If the program will not be using the symbols of the required library, it should not be linking directly to that library.

我理解其中的含义,但我不完全理解这两种情况下的链接过程有何不同。 即给定这两个版本的 *.pc,链接过程将如何进行?

bar1.pc:

prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: bar
Description: The bar library
Version: 2.1.2
Requires.private: foo >= 0.7
Cflags: -I${includedir}
Libs: -L${libdir} -lbar

bar2.pc:

prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib

Name: bar
Description: The bar library
Version: 2.1.2
Requires: foo >= 0.7
Cflags: -I${includedir}
Libs: -L${libdir} -lbar

最佳答案

对于动态链接,所有Requires 库都将成为程序 的依赖项(app/lib 链接到您的lib)。如果您使用 Requires.private,只有您的库会链接依赖项,而不是程序。

需要:

+----------------+   +----------------+   +---------------+
| program        |-->| your lib       |-->| required lib  |
|                |   +----------------+   |               |
|                |----------------------->|               |
+----------------+                        +---------------+

使用Requires.private:

+----------------+   +----------------+   +---------------+
| program        |-->| your lib       |-->| required lib  |
+----------------+   +----------------+   +---------------+

关于shared-libraries - 在 pkg-config 中使用 Requires 与 Requires.private 时,链接过程有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45441250/

相关文章:

c++ - PLT 代码中的段错误。任何想法为什么?

c++ - 简单共享库

c++ - 在 Windows 和 LINUX 中创建程序库 [C++]

c - 使用 gcc 命令行从 .c 文件构建 .so 文件

opencv - pkg-config --cflags opencv : No such file or directory

opencv - 将 OpenCV 链接到 Webots(Ubuntu) 时出现问题

linux - ld 找不到现有的库

ubuntu - 设置一个 debian 包以执行安装操作

c - 在 Fedora 16 中找不到 glib.h

linux - 安装 usblib 包 - Ubuntu