Using cmake language, say I create a target foo
, and link it to libbar.so
. libbar.so
was already compiled on a different platform with a cmake PRIVATE depedency on libbaz.so
. So the dependency chain is
foo ----> libbar --(PRIVATE)--> libbaz
Does libbaz.so
need to be present on the system when I compile foo
and link it with libbar
? My understanding is that I can compile and link foo
without libbaz
, and then find libbaz
at foo
s runtime on a different platform using LD_LIBRARY_PATH
. In other words, my hunch is that libbaz
is only a runtime, and not compile time dependency.
But my current design for a large build system is dependent on this hunch being correct, so I want to double check.
CodePudding user response:
General linking by the compiler toolchain
In general it depends on how libbaz
got linked to libbar
.
If libbaz
got statically linked when building libbar
then there are no compile or runtime dependencies at build-time of foo
to libbaz
. This is due to the fact that statically linked code gets "copied" into the file that links it (when no special hacks or other dark magic is used).
If libbaz
got dynamically linked when building libbar
then libbaz
is (only) a runtime dependency at build-time of foo
.
final library | linking | middle library | linking | first library | runtime dependencies |
---|---|---|---|---|---|
foo | statically | libbar | statically | libbaz | none |
foo | statically | libbar | dynamically | libbaz | libbaz |
foo | dynamically | libbar | statically | libbaz | libbar |
foo | dynamically | libbar | dynamically | libbaz | libbar, libbaz |
There is an exception. But it should not apply in your specified scenario. If libbar
dynamically loads another library (like libbaz
) via Windows-API LoadLibrary or Unix/Linux/...-API dlopen then the linker does not have any information about this dependency at build-time. In this case it is a dynamic runtime dependency by code.
Public / Private
In this case as you are using CMake it is a terminology of CMake. This is only meaningful when defining dependencies in CMake (mainly via target_link_libraries).
CMake target | linking | CMake target or library | description |
---|---|---|---|
libbar | private | libbaz | libbar gets libbaz' includes, etc. at compile-time and as linking library at link-time. |
libbar | public | libbaz | libbar gets libbaz' includes, etc. at compile-time and as linking library at link-time. Libraries using libbar are also getting the interface portion of libbaz (interface includes, libs, etc.). |
libbar | interface | libbaz | libbar does not use libbaz at all. Libraries using libbar are getting the interface portion of libbaz (interface includes, libs, etc.). |