我有一个像这样的项目:
main
|- CMakeLists.txt
|- src
|- api_1
| |- CMakeLists.txt (#1)
| |- api_1.h
| |- api_1.cpp
|
|- api_2
|- CMakeLists.txt (#2)
|- api_2.h
|- api_2.cpp
编译probject时,我想获得以下对象
结果是:
executable
libapi_1.so / libapi_1.a
libapi_2.so / libapi_2.a
该项目当前正在构建
libapi_1.so
/ libapi_1.a
,但不会构建其他两个。由于probject的复杂性非常高且命名不明确,我无法理解在哪里/如何适当地修改主级别CMakeLists.txt
以获得额外的.so / .a
文件。有人可以向我提供有关所需内容的明确说明吗?
最佳答案
您可以在CMakeLists.txt中设置2个(或更多)项目:
project(FullProject)
# config...
add_executable(exec1 ${sources_files}) # This line will create a new executable target
target_link_libraries(exec1 lib_you_need)
# config exec1...
# Now defines another target which is a library
add_library(lib1 ${sources_files}) # This line will create a new library target
target_include_directories(lib1 PRIVATE path_you_need)
构建时,您将有两个目标:exec1和lib1,并且可以使用以下命令构建目标:
cmake --build . --target your_target # exec1 or lib1
要么
make your_target # exec1 or lib1