0

If a C program uses a library A (.a or .so), and the library A uses another library B (.a or .so), when I compile the C program, do I have to specify the path to the header and the library file of library B to a compiler such as gcc?

Thanks.

  • Depends on the libraries and your program. If your program is indirectly/transitively including headers from `B`, then yeah, you'll need the header path. Depending on how the `A` library was built and linked, you may or may not need to explicitly link to the `B` library as well. – Cornstalks Oct 29 '17 at 00:02
  • Thanks. Can you elaborate on "Depending on how the A library was built and linked, you may or may not need to explicitly link to the B library as well."? –  Oct 29 '17 at 00:04
  • Generally, if `A` is a shared library, you typically don't need to pass `B` to the linker. For static libraries, It's possible to pre-link `A`. Or you can make `A` be a composite/conglomerate library (so it includes `B`). Or you can not do anything fancy and require `B` be passed to the linker when using `A`. – Cornstalks Oct 29 '17 at 01:53
  • Thanks. (1) "if A is a shared library, you typically don't need to pass B to the linker." How can B be found? (2) What is "pre-link A"? (3) How can you "make A be a composite/conglomerate library (so it includes B)"? –  Oct 29 '17 at 02:22
  • (1) If B is a static library, it's already linked into A. If B is a dynamic library, then A can specify that it links to it (and where to find it) (try running `ldd` (Linux) or `otool -L` (macOS) on a shared library to see what it links to). (2) You can link A's and B's [.o files into a single .o file with `ld -r`](https://stackoverflow.com/q/2980102/1287251). (3) Use a thin archive, or [a little `ar` script](https://stackoverflow.com/q/3821916/1287251#comment36843318_23621751). – Cornstalks Oct 29 '17 at 02:34

1 Answers1

0

do I have to specify the path to the header

This depends if the header (#include) file of library A contains an #include line which includes the header file of library B.

Most developers of libraries try to avoid this so you can even use library A if you do not have the header files of library B!

(Which of course means that you do not have to specify the path to the header files.)

and the library file of library B to a compiler such as gcc?

  • If library A is a static library (.a) you definitely have to specify library B:

    It does not matter if library B is static (.a) or shared (.so). However if library B is static (.a) and uses a library C you also have to specify library C. And if library C uses a library D...

  • If library A is a shared library (.so) it depends on the operating system used:

    • Using very, very old Linux variants (late 1990s) you had to specify library B (and library C ...).

    • Maybe there are still operating systems which have this behaviour.

    • Under recent Linux variants you don't have to specify library B if library A is a shared library. The same is true for (all versions of?) Windows.

Martin Rosenau
  • 17,897
  • 3
  • 19
  • 38