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

How does the linking process differ when using Requires vs Requires.private in 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

对于动态 linking,所有 Requires 库将成为 程序 的依赖项(app/lib linking针对你的 lib)。如果你使用 Requires.private,只有你的库会 link 反对依赖,而不是程序。

Requires:

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

Requires.private:

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