问题描述
我正在尝试设置项目以使用CMake正确编译。
I'm trying to set up my project to compile correctly using CMake.
我的目录如下:
root
|- bin
| |- // Where I want to build CMake from - using 'cmake ..'
|- build
| |-
|- include
| |- database
| | |- database.h
|- src
|- database
| |- database.cpp
|- main
|- main.cpp
随着项目的扩大,我的子目录肯定会增长,并且认为CMake可能是一个好主意。目前,我只能让CMake在src /中没有子目录的情况下工作。但是,我确实希望该项目能够成长为许多子目录。
My sub-directories will definitely grow as my project grows larger and figured CMake would probably a good idea to learn. At the moment I can only get CMake to work with no sub-directories inside my src/. However, I do expect this project to grow into many sub-directories.
在每个包含.cpp文件的目录中是否需要多个 CMakeLists.txt ?有人可以指出我正确的方向吗?
Would I need multiple CMakeLists.txt inside each directory with .cpp files? Can anyone point me in the right direction?
谢谢!
推荐答案
您的项目似乎不需要多个CMakeLists.txt。多次拥有多个CMakeLists.txt的目的是多重的,但似乎不适合您的项目。例如,一种好处是分层组织,因此您可以将不同的 单位 分开进行构建,并最终进行链接。
Your project does not seem to need multiple CMakeLists.txt. The purpose of having multiple CMakeLists.txt multiple times is multi-fold, but it doesn't seem to fit in your project. For example, one benefit would be hierarchical organization, so that you can separate different units to be built separately, and linked eventually.
示例
请考虑以下情况:如果您的根目录中有另一个名为 tests $的目录c $ c>。该目录包含您希望开发人员选择是否编译它的单元测试。在这种情况下,您将一个CMakeLists.txt放入
tests
,然后在主CMakeLists.txt中使用如下:
Consider the situation if you have another directory in your root called tests
. That directory contains unit tests that you want the developer to have the choice whether to compile it or not. In that case, you put one CMakeLists.txt in tests
, and you enable this in your primary CMakeLists.txt with a add_subdirectory(tests)
as so:
if(testing_enabled)
add_subdirectory(tests)
endif()
在测试
中,还有另一个CMakeLists.txt。这样,您就可以分离关注点。单元测试的开发独立于项目的开发,并且构建过程是独立的。
In tests
, there would be another CMakeLists.txt. This way, you separate concerns. The development of your unit tests is independent of the development of your project, and also the build process is separate.
另一个示例
请考虑以下情况:如果您有两个可供用户选择的库。在这种情况下,您在根
中有两个目录, lib1
和 lib2
。现在,您可以在主CMakeLists.txt中执行以下操作:
Consider the situation if you have two libraries where the user can choose from. In this case, you have in your root
two directories, lib1
and lib2
. Now you can do something like this in your primary CMakeLists.txt:
if(IWantLib1)
add_subdirectory(lib1)
else()
add_subdirectory(lib2)
endif()
并且 lib1
和 lib2
目录都包含CMakeLists.txt。
And both lib1
and lib2
directories contain CMakeLists.txt.
这篇关于带子目录的CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!