添加在CMake的多个可执行文件 [英] Adding multiple executables in CMake
问题描述
在C ++项目我的code的结构如下
My code in a C++ project is organised as follows
- 我有几个
的.cpp
和.H
文件,其中包含我的班 - 我有必须对
的.cpp
文件和一些外部库编译几个.CXX
文件。
- 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.
现在,每个 .CXX
文件中有一个的main()
方法,所以我需要添加一个不同的可执行每个具有相同名称的文件,这些文件的。
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
文件可能不会被链接到同一个外部库。
Also, these .cxx
files might not get linked to the same external libraries.
我想写这个构建于CMake的,其中我是那种一个新手,我怎么去呢?
I want to write this build in CMake, in which I am kind of a newbie, how do I go about this?
推荐答案
我的建议是分两个阶段来解决这样的:
My suggestion is to tackle this in two phases:
- 构建从
的.cpp
和.H
文件库,使用<一个href=\"http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command%3aadd_library\"><$c$c>add_library$c$c> - 通过所有迭代的
.CXX
文件,并创建从每一个可执行文件,使用<一个href=\"http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command%3aadd_executable\"><$c$c>add_executable$c$c>和<一个href=\"http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command%3aforeach\"><$c$c>foreach$c$c>
- 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
这可能是一些简单的
file( GLOB LIB_SOURCES lib/*.cpp )
file( GLOB LIB_HEADERS lib/*.h )
add_library( YourLib ${LIB_SOURCES} ${LIB_HEADERS} )
构建所有的可执行文件
只要在所有.cpp文件回路和创建独立的可执行文件。
Build all the executables
Simply loop over all the .cpp files and create separate executables.
# 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} )
某些警告:
-
文件(GLOB)
通常不建议,因为如果添加了一个新文件的CMake不会自动重建。我用在这里,是因为我不知道你的来源档案。 - 在某些情况下,源文件也可以以一个完整路径中。如果有必要,使用
找到标志(GLOB。 ..)
。 - 手动设置源文件需要改变的CMakeLists.txt,这将触发重建。请参见的通配的(解散)的优势这个问题。
- 我生成使用
字符串(替换...)的测试名
。我可以使用<一个href=\"http://www.cmake.org/cmake/help/v2.8.8/cmake.html#command%3aget_filename_component\">get_filename_component与NAME_WE
标记。 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 question for the (dis-)advantages of globbing.
- I generated the testname using a
string( REPLACE ... )
. I could have used get_filename_component with theNAME_WE
flag. - CMake tutorial
- What are the dusty corners a newcomer to CMake will want to know?
Some warnings:
关于一般CMake的信息,我建议你阅读一些在这里已经问计算器上广泛的CMake的概述的问题。例如:
Concerning "general" CMake info, I advise you to read some of the broad "CMake Overview" questions already asked here on stackoverflow. E.g.:
这篇关于添加在CMake的多个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!