Suppose I have a directory structure as follows:
/main.cpp
/CMakeLists.txt
/foo/foo.cpp
/foo/CMakeLists.txt
where my /CMakeLists.txt
file contains the following:
project(Test)
add_subdirectory("foo")
add_executable(Test main.cpp foo/foo.cpp)
target_link_libraries(Test ${OpenNI_LIB})
and my /foo/CMakeLists.txt
file contains the following:
find_library(OpenNI REQUIRED)
When I use the line add_subdirectory("foo")
in the first CMakeLists.txt
, what actually happens? Does it search for a second CMakeLists.txt
file in foo
, and add the contents to the first? Will any variables defined the second file be available in the first? And specifically in this example, will the variable ${OpenNI_LIB}
be recognised in the first, given that it is defined in the second?
Thanks.
Yes, it does
No it doesn't. It performs a number of configuring actions such as finding libraries etc and a generates separate Makefile and/or other build-time artifacts.
no, unless such a construction
is used in
foo/CMakeLists.txt
By default variables defined in a subdirectory's CMakeLists.txt are also defined in the subdirectory's subdirectories, that is if
foo/
in turn containedbar/
with its ownCMakeLists.txt
, then withinbar/
'sCMakeLists.txt
${OpenNI_LIB} would be set.P.S. message(STATUS "Some message with ${VAR}") in doubtful places of
CMakeLists.txt
is your friend. Just look into cmake output.