Thursday, November 11, 2021

[SOLVED] Are the sources added to a library via the add_library command PUBLIC or PRIVATE?

Issue

I'm trying to add some more structure to my CMake project. One step of this process is to move source additions to the CMakeLists.txts in a few subdirectories, whereas they are currently added during target creation via add_library. Unlike add_library, however, target_sources gives you the choice between PUBLIC, INFERFACE, and PRIVATE. The sources added by add_library obviously aren't interfaces, but I'm unsure if they are PUBLIC or PRIVATE.


Solution

CMake command add_library interprets its immediate sources as PRIVATE: the sources belongs only to the created target and aren't propagated to the target linked with the library.

In general, non-PRIVATE sources has a very limited usage. If two or more targets are linked together and share a source file, then linker usually reports "multiple definitions" error about symbols defined in that file.



Answered By - Tsyvarev