How to change the volume of imported library using CMake

CMake question,

The imported library has scope in the directory in which it was created and below.

If I want to use this library in the parent scope, what should I do?


top CMakeLists.txt

add_executable(myapp main.cpp)
target_link_libraries(myapp imported_lib)


sub CMakeLists.txt

add_library(imported_lib STATIC IMPORTED)


Thanks for your help ~~


source to share

2 answers

Unlike classic libraries, imported libraries are linked to a directory. This can be changed using options GLOBAL


Here is an extract from the documentation :

The target name is scoped in the directory in which it is created and below, but the GLOBAL option expands its visibility.


add_library(imported_lib STATIC IMPORTED GLOBAL)


As far as I can tell, this option has always been available.



You don't need to do anything, CMake will automatically resolve this dependency. See the documentation for add_subdirectory :

If the target created by the parent project depends on the target in a subdirectory, the dependent target will be included in the parent project's build system to satisfy the dependency.

This is contrary to the set () and list () commands, which require an explicit PARENT_SCOPE parameter to be passed.



All Articles