C++ 在 CMake 中添加多个可执行文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14306642/
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
Adding multiple executables in CMake
提问by ssb
My code in a C++ project is organised as follows
我在一个C++项目中的代码组织如下
- I have several
.cpp
and.h
files which contains my classes - I have several
.cxx
files which have to be compiled against the.cpp
files and some external libraries.
- 我有几个
.cpp
和.h
文件,其中包含我的班 - 我有几个
.cxx
文件必须针对这些.cpp
文件和一些外部库进行编译。
Now, each of the .cxx
files have a main()
method, so I need to add a different executable for each of these files having the same name as the file.
现在,每个.cxx
文件都有一个main()
方法,所以我需要为每个与文件同名的文件添加一个不同的可执行文件。
Also, these .cxx
files might not get linked to the same external libraries.
此外,这些.cxx
文件可能不会链接到相同的外部库。
I want to write this build in CMake, in which I am kind of a newbie, how do I go about this?
我想在 CMake 中编写这个构建,我是一个新手,我该怎么做?
回答by André
My suggestion is to tackle this in two phases:
我的建议是分两个阶段解决这个问题:
- Build a library from the
.cpp
and.h
files, usingadd_library
- Iterate through all your
.cxx
files and create an executable from each, usingadd_executable
andforeach
- 从
.cpp
和.h
文件构建一个库,使用add_library
- 遍历所有
.cxx
文件并从每个文件创建一个可执行文件,使用add_executable
和foreach
Build the library
建立图书馆
This could be something as simple as
这可能很简单
file( GLOB LIB_SOURCES lib/*.cpp )
file( GLOB LIB_HEADERS lib/*.h )
add_library( YourLib ${LIB_SOURCES} ${LIB_HEADERS} )
Build all the executables
构建所有可执行文件
Simply loop over all the .cpp files and create separate executables.
只需循环遍历所有 .cpp 文件并创建单独的可执行文件。
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
# file( GLOB APP_SOURCES RELATIVE app/*.cxx )
file( GLOB APP_SOURCES app/*.cxx )
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
# Make sure YourLib is linked to each app
target_link_libraries( ${testname} YourLib )
endforeach( testsourcefile ${APP_SOURCES} )
Some warnings:
一些警告:
file( GLOB )
is usually not recommended, because CMake will not automatically rebuild if a new file is added. I used it here, because I do not know your sourcefiles.- In some situations, source-files may be found with a full pathname. If necessary, use the RELATIVE flag for
find( GLOB ... )
. - Manually setting the source-files requires a change to CMakeLists.txt, which triggers a rebuild. See this questionfor the (dis-)advantages of globbing.
- I generated the testname using a
string( REPLACE ... )
. I could have used get_filename_componentwith theNAME_WE
flag.
file( GLOB )
通常不推荐,因为如果添加新文件,CMake 不会自动重建。我在这里使用它,因为我不知道你的源文件。- 在某些情况下,可能会找到带有完整路径名的源文件。如有必要,请为 使用RELATIVE 标志
find( GLOB ... )
。 - 手动设置源文件需要更改 CMakeLists.txt,这会触发重建。请参阅此问题以了解通配的 (dis-) 优势。
- 我使用
string( REPLACE ... )
. 我本可以使用带有标志的get_filename_componentNAME_WE
。
Concerning "general" CMake info, I advise you to read some of the broad "CMake Overview" questions already asked here on stackoverflow. E.g.:
关于“一般”CMake 信息,我建议您阅读这里已经在 stackoverflow 上提出的一些广泛的“CMake 概述”问题。例如: