cmake项目仅构建一个特定的可执行文件(及其依赖项) [英] cmake project build only one specific executable (and its dependencies)

查看:86
本文介绍了cmake项目仅构建一个特定的可执行文件(及其依赖项)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个大致如此结构的CMake项目:

 
|-库1
| |-CMakeLists.txt
|-library2
| |-CMakeLists.txt
|-可执行文件
| |-CMakeLists.txt

可执行文件中我生成我的2个可执行文件。我想知道是否有可能仅生成一个可执行文件及其依赖项而不是全部。我听到了有关cmake -target 选项的信息,但是我无法使其与 cmake / 3.13.4 一起使用。

解决方案

这里有两个潜在的问题。首先,典型的CMake工作流程将 build 文件夹放置为顶级CMake文件的同级文件。因此,您的文件层次结构应如下所示:

 
|-CMakeLists.txt
|-library1
|- |-CMakeLists.txt
|-library2
| |-CMakeLists.txt
|-可执行文件
| |-CMakeLists.txt
|-build< ------------从这里运行CMake命令。

这会将所有CMake生成的文件隔离到 build 文件夹。其次,您必须小心在适当的位置运行CMake构建阶段。我们可以从 build 文件夹中运行所有内容,例如:


  1. 生成构建系统,请从 build 目录运行 cmake .. 。第一步应指向顶级CMake文件。


  2. 构建(或编译)特定目标,说它是名为 MyExecutable1 ,运行以下命令:

      cmake --build。 --target MyExecutable1 

    来自 build 目录。您必须确保将-build 标志指向 build 文件夹,这次是顶级CMake文件。另外,此命令中要指定的目标名称应与 add_executable()项目名称或其他任何名称中使用的目标名称相匹配。 / p>


  3. 和往常一样,在尝试运行CMake时遇到错误/问题时,它有助于清除缓存(删除 build / CMakeCache.txt ),或者只是完全删除 build 文件夹并重新开始。



I have a CMake Project with roughly this structure:

.
|-- library1
|   |-- CMakeLists.txt
|-- library2
|   |-- CMakeLists.txt
|-- executables
|   |-- CMakeLists.txt

in executables i generate my 2 executables. I wonder if it was possible just generating one executable and its dependencies instead of all. I heard something about the cmake --target option but I cant get it to work with cmake/3.13.4.

解决方案

There are a couple potential issues here. First, the typical CMake workflow places the build folder as a sibling to the top-level CMake file. So your file hierarchy should look something like this:

.
|-- CMakeLists.txt
|-- library1
|   |-- CMakeLists.txt
|-- library2
|   |-- CMakeLists.txt
|-- executables
|   |-- CMakeLists.txt
|-- build    <------------ Run CMake commands from here.

This isolates all the CMake-generated files to the build folder. Secondly, you must to careful to run the CMake build stages in the proper location. We can run everything from within the build folder, for example:

  1. To generate the build system, run cmake .. from the build directory. This first step should point to the top-level CMake file.

  2. To build (or compile) a specific target, say it's called MyExecutable1, run this:

    cmake --build . --target MyExecutable1
    

    from the build directory. You must be sure to point the --build flag at the build folder, not the top-level CMake file this time. Also, the target name to specify in this command should match the target name used in add_executable(), not the project name or anything else.

  3. As always, when getting errors/issues while attempting to run CMake, it helps to clear the cache (delete build/CMakeCache.txt), or just delete the build folder altogether and start fresh.

这篇关于cmake项目仅构建一个特定的可执行文件(及其依赖项)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆