1, Introduction
This command is to add a subdirectory and build the subdirectory. The command format is
add_subdirectory (source_dir [binary_dir] [EXCLUDE_FROM_ALL])
Command parsing
- source_dir
Required parameter. This parameter specifies a subdirectory that should contain cmakelists Txt file and code file. The subdirectory can be a relative path or an absolute path. If it is a relative path, it is a relative path relative to the current directory. - binary_dir
Optional parameters. This parameter specifies a directory for storing output files. It can be a relative path or an absolute path. If it is a relative path, it is a relative path relative to the current output directory. If this parameter is not specified, the default output directory uses source_dir. - EXCLUDE_FROM_ALL
Optional parameters. When this parameter is specified, the target under the subdirectory will not be included by the target file under the parent directory, and the cmakelists of the parent directory Txt will not build the target file of the subdirectory, but must be explicitly built under the subdirectory. Exception: when the target of the parent directory depends on the target of the subdirectory, the target of the subdirectory will still be built to meet the dependency (for example, target_link_libraries is used).
2, Instance
The directory structure and description are as follows:
├── CMakeLists.txt # cmakelist of parent directory txt
├── main.cpp # source file, including main function
♪ sub # subdirectory
└── CMakeLists.txt # subdirectory cmakelists txt
└── test.h # subdirectory header file
└── test.cpp # subdirectory source file
Test. Under subdirectory sub CPP defines a function test(), which prints out the input parameters and the corresponding header file test H declares test(), cmakelists Txt compiles the source file under sub into a library file.
// sub/test.cpp #include "test.h" #include <iostream> void test(std::string str) { std::cout << str << std::endl; }
// sub/test.h #include <string> void test(std::string str);
# sub/CMakeLists.txt cmake_minimum_required (VERSION 2.8) project(sub) add_library(sub test.cpp)
- Scenario 1: parent directory cmakelists Txt add_subdirectory specifies only source_dir.
# Cmakelists under the parent directory txt cmake_minimum_required (VERSION 2.8) project(test) add_subdirectory(sub)
Call cmake under the parent directory After the build, libsub. Sub will appear in the sub directory A library, indicating that when binary is not specified_ Dir, the output target file will be placed in source_dir directory.
- Scenario 2: parent directory cmakelists Txt add_subdirectory specifies source_dir and binary_dir.
# Cmakelists under the parent directory txt cmake_minimum_required (VERSION 2.8) project(test) add_subdirectory(sub output)
Call cmake under the parent directory After the build, libsub. Sub will appear in the output directory A library, and there is no libsub in the sub directory a. Description when binary is specified_ Dir, the output target file will be placed in binary_dir directory.
Scenario 3: parent directory cmakelists Txt add_subdirectory specifies EXCLUDE_FROM_ALL option.
# Cmakelists under the parent directory txt cmake_minimum_required (VERSION 2.8) project(test) add_subdirectory(sub output EXCLUDE_FROM_ALL) add_executable(test main.cpp)
Call cmake under the parent directory After the build, libsub will not appear in the output directory or sub directory A library. When exclude is specified_ FROM_ With the all option, the target file of the subdirectory will not be generated.
- Scenario 4: parent directory cmakelists Txt add_subdirectory specifies EXCLUDE_FROM_ALL option, and the target file of the parent directory depends on the target file of the subdirectory.
# Cmakelists under the parent directory txt cmake_minimum_required (VERSION 2.8) project(test) add_subdirectory(sub output EXCLUDE_FROM_ALL) add_executable(test main.cpp) target_link_libraries(test sub)
Call cmake in the parent directory After the build, libsub. Sub will appear in the output directory A library, even if exclude is specified_ FROM_ With the all option, when the parent directory target file has a dependency on the subdirectory target file, the target file of the subdirectory will still be generated to meet the dependency.
Finally, this article ends with a complete example (the contents of CMakeList.txt, test.h, test.cpp and other files in the sub directory are as shown above, and there is no change), and the main CPP and cmakelist Txt as follows:
# Cmakelists under the parent directory txt cmake_minimum_required (VERSION 2.8) project(test) include_directories(sub) add_subdirectory(sub output) add_executable(test main.cpp) target_link_libraries(test sub)
# Main. In the parent directory cpp #include "test.h" #include <iostream> int main(int argc, char** argv) { std::cout << "In main..." << std::endl; test("hello, world!"); return 0; }
# output > cmake --build . Scanning dependencies of target sub [ 25%] Building CXX object output/CMakeFiles/sub.dir/test.cpp.o [ 50%] Linking CXX static library libsub.a [ 50%] Built target sub Scanning dependencies of target test [ 75%] Building CXX object CMakeFiles/test.dir/main.cpp.o [100%] Linking CXX executable test [100%] Built target test >./test In main... hello, world!
reference resources:
add_subdirectory — CMake 3.22.1 Documentation
Add of Cmake command_ Subdirectory introduction - brief book