• <tfoot id='vTAyh'></tfoot>

      <bdo id='vTAyh'></bdo><ul id='vTAyh'></ul>
  • <small id='vTAyh'></small><noframes id='vTAyh'>

  • <i id='vTAyh'><tr id='vTAyh'><dt id='vTAyh'><q id='vTAyh'><span id='vTAyh'><b id='vTAyh'><form id='vTAyh'><ins id='vTAyh'></ins><ul id='vTAyh'></ul><sub id='vTAyh'></sub></form><legend id='vTAyh'></legend><bdo id='vTAyh'><pre id='vTAyh'><center id='vTAyh'></center></pre></bdo></b><th id='vTAyh'></th></span></q></dt></tr></i><div id='vTAyh'><tfoot id='vTAyh'></tfoot><dl id='vTAyh'><fieldset id='vTAyh'></fieldset></dl></div>
      <legend id='vTAyh'><style id='vTAyh'><dir id='vTAyh'><q id='vTAyh'></q></dir></style></legend>

        在 CMake 中添加多个可执行文件

        时间:2023-08-26
      1. <i id='YcqVm'><tr id='YcqVm'><dt id='YcqVm'><q id='YcqVm'><span id='YcqVm'><b id='YcqVm'><form id='YcqVm'><ins id='YcqVm'></ins><ul id='YcqVm'></ul><sub id='YcqVm'></sub></form><legend id='YcqVm'></legend><bdo id='YcqVm'><pre id='YcqVm'><center id='YcqVm'></center></pre></bdo></b><th id='YcqVm'></th></span></q></dt></tr></i><div id='YcqVm'><tfoot id='YcqVm'></tfoot><dl id='YcqVm'><fieldset id='YcqVm'></fieldset></dl></div>

                <legend id='YcqVm'><style id='YcqVm'><dir id='YcqVm'><q id='YcqVm'></q></dir></style></legend>

                <small id='YcqVm'></small><noframes id='YcqVm'>

              • <tfoot id='YcqVm'></tfoot>
                  <tbody id='YcqVm'></tbody>
                • <bdo id='YcqVm'></bdo><ul id='YcqVm'></ul>
                  本文介绍了在 CMake 中添加多个可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在一个C++项目中的代码组织如下

                  My code in a C++ project is organised as follows

                  • 我有几个 .cpp.h 文件,其中包含我的类
                  • 我有几个 .cxx 文件,它们必须针对 .cpp 文件和一些外部库进行编译.
                  • 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:

                  1. .cpp.h 文件构建一个库,使用 add_library
                  2. 遍历所有 .cxx 文件并使用 add_executableforeach
                  1. Build a library from the .cpp and .h files, using add_library
                  2. Iterate through all your .cxx files and create an executable from each, using add_executable and foreach

                  构建库

                  这可能很简单

                  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} )
                  

                  一些警告:

                  • file( GLOB ) 通常不推荐使用,因为如果添加新文件,CMake 不会自动重建.我在这里使用它,因为我不知道你的源文件.
                  • 在某些情况下,可能会找到带有完整路径名的源文件.如有必要,请为 find(全局...).
                  • 手动设置源文件需要更改 CMakeLists.txt,这会触发重建.请参阅这个问题,了解通配的 (dis-) 优势.
                  • 我使用 string( REPLACE ... ) 生成了测试名称.我可以使用 get_filename_component 和 NAME_WE 标志.
                  • 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 question for the (dis-)advantages of globbing.
                    • I generated the testname using a string( REPLACE ... ). I could have used get_filename_component with the NAME_WE flag.
                    • 关于一般"CMake 信息,我建议您阅读一些已在 stackoverflow 上提出的广泛的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 教程
                      • 什么是CMake 新手会想知道的尘土飞扬的角落吗?

                      这篇关于在 CMake 中添加多个可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何检测 TCP 连接中的网线已被拔掉? 下一篇:如何使用 CMake 在 VS2017 中启用/std:c++17

                  相关文章

                    <bdo id='yKr1j'></bdo><ul id='yKr1j'></ul>

                  1. <tfoot id='yKr1j'></tfoot>

                      <small id='yKr1j'></small><noframes id='yKr1j'>

                      <legend id='yKr1j'><style id='yKr1j'><dir id='yKr1j'><q id='yKr1j'></q></dir></style></legend>
                      <i id='yKr1j'><tr id='yKr1j'><dt id='yKr1j'><q id='yKr1j'><span id='yKr1j'><b id='yKr1j'><form id='yKr1j'><ins id='yKr1j'></ins><ul id='yKr1j'></ul><sub id='yKr1j'></sub></form><legend id='yKr1j'></legend><bdo id='yKr1j'><pre id='yKr1j'><center id='yKr1j'></center></pre></bdo></b><th id='yKr1j'></th></span></q></dt></tr></i><div id='yKr1j'><tfoot id='yKr1j'></tfoot><dl id='yKr1j'><fieldset id='yKr1j'></fieldset></dl></div>