C++ 多库 CMakeLists.txt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8474582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple library CMakeLists.txt
提问by Cartesius00
We have a project P (C/C++
on Linux) consisting of libraries lib1
, lib2
, lib3
.
我们有一个项目 P(C/C++
在 Linux 上)由库lib1
、lib2
、lib3
。
lib1
is standalone linked to another system-wide libslib2
links tolib1
lib3
links to bothlib1
andlib2
lib1
独立链接到另一个系统范围的库lib2
链接到lib1
lib3
链接到两者lib1
和lib2
We have a directory P and extra directories for each of our libs (so, P/lib1/
, P/lib2/
...). Every library has also its own tests.
我们有一个目录 P 和每个库的额外目录(so, P/lib1/
, P/lib2/
...)。每个图书馆也有自己的测试。
Questions:
问题:
- Please, how to organize
CMakeLists.txt
for this scenario? - Should we create only one master
build
directory or one for each lib? - Can we have an option in
CMakeLists.txt
forSTATIC vs. SHARED
linking?
- 请问,如何组织
CMakeLists.txt
这种情况? - 我们应该只创建一个主
build
目录还是为每个库创建一个? - 我们可以有一个选择
CMakeLists.txt
的STATIC vs. SHARED
链接?
回答by André
In this case, I would recommend using a single build/ directory. CMake will likely generate separate lib1, lib2 and lib3 directories in there.
在这种情况下,我建议使用单个 build/ 目录。CMake 可能会在那里生成单独的 lib1、lib2 和 lib3 目录。
Switching between STATIC vs. SHARED can be done by using the BUILD_SHARED_LIBS flag (check the add_librarydocumentation)
可以使用 BUILD_SHARED_LIBS 标志在 STATIC 和 SHARED 之间切换(查看add_library文档)
With respect to the CMakeLists.txt organization, the choice is yours:
关于 CMakeLists.txt 组织,选择权在您手中:
You can build a single CMakeLists.txt which has multiple add_libraryentries. This has the benefit that you will get a single CMakeLists.txt, which some people may prefer when the projects are simple.
You could split up your project into multiple CMakeLists.txt distributed over your lib1, lib2 and lib3 directories and use a root cmakelists.txt with add_subdirectory. The benefit of this setup is that it will be easier to generate the build-files with one call (in your build/ directory), but you could then easily step into e.g. lib3/ and call make/msbuild there. CMake will ensure that the dependencies are built correctly
您可以构建具有多个add_library条目的单个 CMakeLists.txt 。这样做的好处是您将获得单个 CMakeLists.txt,当项目很简单时,有些人可能更喜欢它。
您可以将项目拆分为分布在 lib1、lib2 和 lib3 目录中的多个 CMakeLists.txt,并使用带有add_subdirectory的根 cmakelists.txt 。这种设置的好处是通过一次调用(在您的 build/ 目录中)可以更容易地生成构建文件,但是您可以轻松地进入例如 lib3/ 并在那里调用 make/msbuild。CMake 将确保正确构建依赖项
Example 1:
示例 1:
project( P )
# Setup lib1
set ( LIB1_SOURCES ... ) # Fill in your set of source-files here...
add_library( lib1 ${LIB1_SOURCES} )
# Do similar for lib2 and lib3
target_link_libraries( lib2 lib1 ) # Indicate that lib1 is needed for lib2
target_link_libraries( lib3 lib1 lib2 ) # Indicate that lib2 and lib1 are needed for lib3
Example 2:
示例 2:
project( P )
add_subdirectory( lib1 )
add_subdirectory( lib2 )
add_subdirectory( lib3 )
In each subdirectory you then write your CMakeLists.txt. E.g. in case of lib3:
然后在每个子目录中编写 CMakeLists.txt。例如在 lib3 的情况下:
project( lib3 )
set( LIB3_SOURCES ... ) # Setup the list of sources here.
add_library( lib3 ${LIB3_SOURCES} )
# You can refer to other libraries which should be available from the root cmakelists.
target_link_libraries( lib3 lib1 lib2 )