How does the linking process differ when using Requires vs Requires.private in pkg-config?
Asked Answered
C

1

5

I'm referring to this guide to pkg-config for learning how to write one.

At one place, it mentions the following about the Requires and Requires.private fields.

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.

I understand the implications, but I don't compltely understand how will the linking process differ in the two cases. i.e given these two versions of *.pc, how will the linking process work?

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
Confraternity answered 1/8, 2017 at 14:56 Comment(0)
R
6

For dynamic linking, all Requires libraries will become dependencies of the program (the app/lib linking against your lib). If you use Requires.private, only your lib will link against the dependency, but not the program.

With Requires:

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

With Requires.private:

+----------------+   +----------------+   +---------------+
| program        |-->| your lib       |-->| required lib  |
+----------------+   +----------------+   +---------------+
Renault answered 8/5, 2020 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.