CMake 中的调试与发布

发布于 2024-12-09 08:32:11 字数 183 浏览 0 评论 0 原文

在 GCC 编译的项目中,

  • 如何为每个目标类型(调试/发布)运行 CMake?
  • 如何使用 CMake 指定调试和发布 C/C++ 标志?
  • 如何表达主可执行文件将使用 g++ 进行编译,而一个嵌套库将使用 gcc 进行编译?

In a GCC compiled project,

  • How do I run CMake for each target type (debug/release)?
  • How do I specify debug and release C/C++ flags using CMake?
  • How do I express that the main executable will be compiled with g++ and one nested library with gcc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

陌路黄昏 2024-12-16 08:32:11

对于 CMake,通常建议执行 "out源”构建。在项目的根目录中创建 CMakeLists.txt。然后从项目的根目录:

mkdir Release
cd Release
cmake -DCMAKE_BUILD_TYPE=Release ..
make

对于 Debug (再次从项目的根目录):

mkdir Debug
cd Debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
make

Release / Debug 将添加适当的标志为你的编译器。还有 RelWithDebInfoMinSizeRel 构建配置。


您可以通过指定 工具链文件 您可以在其中添加 CMAKE__FLAGS__INIT 变量,例如:

set(CMAKE_CXX_FLAGS_DEBUG_INIT "-Wall")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-Wall")

参见 CMAKE_BUILD_TYPE 了解更多详细信息。


至于你的第三个问题,我不知道你到底想问什么。 CMake 应自动检测并使用适合不同源文件的编译器。

With CMake, it's generally recommended to do an "out of source" build. Create your CMakeLists.txt in the root of your project. Then from the root of your project:

mkdir Release
cd Release
cmake -DCMAKE_BUILD_TYPE=Release ..
make

And for Debug (again from the root of your project):

mkdir Debug
cd Debug
cmake -DCMAKE_BUILD_TYPE=Debug ..
make

Release / Debug will add the appropriate flags for your compiler. There are also RelWithDebInfo and MinSizeRel build configurations.


You can modify/add to the flags by specifying a toolchain file in which you can add CMAKE_<LANG>_FLAGS_<CONFIG>_INIT variables, e.g.:

set(CMAKE_CXX_FLAGS_DEBUG_INIT "-Wall")
set(CMAKE_CXX_FLAGS_RELEASE_INIT "-Wall")

See CMAKE_BUILD_TYPE for more details.


As for your third question, I'm not sure what you are asking exactly. CMake should automatically detect and use the compiler appropriate for your different source files.

芸娘子的小脾气 2024-12-16 08:32:11

这里的很多答案都已经过时/不好。所以我将尝试更好地回答这个问题。当然,我将在 2020 年回答这个问题,所以预计情况会发生变化。


如何为每种目标类型(调试/发布)运行 CMake?

首先,调试/发布在 cmake 中称为配置 (nitpick)。

如果您使用单个配置生成器 (Ninja/Unix-Makefiles),则必须指定 CMAKE_BUILD_TYPE

像这样:

# Configure the build
cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Debug

# Actually build the binaries
cmake --build build/

# Configure a release build
cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Release

# Build release binaries
cmake --build build/

对于多配置生成器,它略有不同(Ninja Multi-Config,Visual Studio)

# Configure the build
cmake -S . -B build

# Build debug binaries
cmake --build build --config Debug

# Build release binaries
cmake --build build --config Release

如果您想知道为什么这是必要的,那是因为 cmake 不是构建系统。它是一个元构建系统(即构建构建系统的构建系统)。这基本上是处理在 1 次构建中支持多种配置的构建系统的结果。如果您想更深入地了解,我建议您阅读 Craig Scott 的书《Professional CMake:实用指南》中有关 cmake 的一些内容


如何使用 CMake 指定调试和发布 C/C++ 标志?

现代实践是使用目标和属性。

这是一个示例:

add_library(foobar)

# Add this compile definition for debug builds, this same logic works for
# target_compile_options, target_link_options, etc.
target_compile_definitions(foobar PRIVATE
    
lt;
lt;CONFIG:Debug>:
        FOOBAR_DEBUG=1
    >
)

注意:我如何使用生成器表达式来指定配置!
使用 CMAKE_BUILD_TYPE 将导致任何多配置生成器的错误构建!

此外,有时您需要在全球范围内进行设置,而不仅仅是针对一个目标。
使用 add_compile_definitions、add_compile_options 等。这些函数支持生成器表达式。除非必须,否则不要使用旧式 cmake(那条路是噩梦之地)


我如何表达主要可执行文件将使用 g++ 编译,并使用 gcc 编译一个嵌套库?

你的最后一个问题真的没有意义。

A lot of the answers here are out of date/bad. So I'm going to attempt to answer it better. Granted I'm answering this question in 2020, so it's expected things would change.


How do I run CMake for each target type (debug/release)?

First off Debug/Release are called configurations in cmake (nitpick).

If you are using a single configuration generator (Ninja/Unix-Makefiles) you must specify the CMAKE_BUILD_TYPE.

Like this:

# Configure the build
cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Debug

# Actually build the binaries
cmake --build build/

# Configure a release build
cmake -S . -B build/ -D CMAKE_BUILD_TYPE=Release

# Build release binaries
cmake --build build/

For multi-configuration generators it's slightly different (Ninja Multi-Config, Visual Studio)

# Configure the build
cmake -S . -B build

# Build debug binaries
cmake --build build --config Debug

# Build release binaries
cmake --build build --config Release

If you are wondering why this is necessary it's because cmake isn't a build system. It's a meta-build system (IE a build system that build's build systems). This is basically the result of handling build systems that support multiple-configurations in 1 build. If you'd like a deeper understanding I'd suggest reading a bit about cmake in Craig Scott's book "Professional CMake: A Practical Guide


How do I specify debug and release C/C++ flags using CMake?

The modern practice is to use target's and properties.

Here is an example:

add_library(foobar)

# Add this compile definition for debug builds, this same logic works for
# target_compile_options, target_link_options, etc.
target_compile_definitions(foobar PRIVATE
    
lt;
lt;CONFIG:Debug>:
        FOOBAR_DEBUG=1
    >
)

NOTE: How I'm using generator expressions to specify the configuration!
Using CMAKE_BUILD_TYPE will result in bad builds for any multi-configuration generator!

Further more sometimes you need to set things globally and not just for one target.
Use add_compile_definitions, add_compile_options, etc. Those functions support generator expressions. Don't use old style cmake unless you have to (that path is a land of nightmares)


How do I express that the main executable will be compiled with g++ and one nested library with gcc?

Your last question really doesn't make sense.

潜移默化 2024-12-16 08:32:11

有关调试/发布标志,请参阅 CMAKE_BUILD_TYPE 变量(您将其作为 cmake -DCMAKE_BUILD_TYPE=value 传递)。它采用诸如 ReleaseDebug 等值。

https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Useful-Variables#compilers-and-tools

cmake 使用扩展来选择编译器,所以只需将您的文件命名为 .c。

您可以使用各种设置覆盖它:

例如:

set_source_files_properties(yourfile.c LANGUAGE CXX) 

使用 g++ 编译 .c 文件。上面的链接还展示了如何为 C/C++ 选择特定的编译器。

For debug/release flags, see the CMAKE_BUILD_TYPE variable (you pass it as cmake -DCMAKE_BUILD_TYPE=value). It takes values like Release, Debug, etc.

https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Useful-Variables#compilers-and-tools

cmake uses the extension to choose the compiler, so just name your files .c.

You can override this with various settings:

For example:

set_source_files_properties(yourfile.c LANGUAGE CXX) 

Would compile .c files with g++. The link above also shows how to select a specific compiler for C/C++.

廻憶裏菂餘溫 2024-12-16 08:32:11

您可以使用 add_compile_options,而不是直接操作 CMAKE_CXX_FLAGS 字符串(使用 string(APPEND CMAKE_CXX_FLAGS_DEBUG " -g3") 可以做得更好) code>:

add_compile_options(
  "-Wall" "-Wpedantic" "-Wextra" "-fexceptions"
  "
lt;
lt;CONFIG:DEBUG>:-O0;-g3;-ggdb>"
)

这会将指定的警告添加到所有构建类型,但仅将给定的调试标志添加到 DEBUG 构建。请注意,编译选项存储为 CMake 列表,它只是一个用分号 ; 分隔其元素的字符串。

Instead of manipulating the CMAKE_CXX_FLAGS strings directly (which could be done more nicely using string(APPEND CMAKE_CXX_FLAGS_DEBUG " -g3") btw), you can use add_compile_options:

add_compile_options(
  "-Wall" "-Wpedantic" "-Wextra" "-fexceptions"
  "
lt;
lt;CONFIG:DEBUG>:-O0;-g3;-ggdb>"
)

This would add the specified warnings to all build types, but only the given debugging flags to the DEBUG build. Note that compile options are stored as a CMake list, which is just a string separating its elements by semicolons ;.

一杯敬自由 2024-12-16 08:32:11

// CMakeLists.txt : 发布

set(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "" FORCE)

// CMakeLists.txt : 调试

set(CMAKE_CONFIGURATION_TYPES "Debug" CACHE STRING "" FORCE)

// CMakeLists.txt : release

set(CMAKE_CONFIGURATION_TYPES "Release" CACHE STRING "" FORCE)

// CMakeLists.txt : debug

set(CMAKE_CONFIGURATION_TYPES "Debug" CACHE STRING "" FORCE)
梦醒时光 2024-12-16 08:32:11

如果您想构建不同的配置而不重新生成,则还可以运行 cmake --build {$PWD} --config 对于多配置工具,请选择 < cfg> 例如。调试、发布、MinSizeRel、RelWithDebInfo

https://cmake .org/cmake/help/v2.8.11/cmake.html#opt%3a--builddir

If you want to build a different configuration without regenerating if using you can also run cmake --build {$PWD} --config <cfg> For multi-configuration tools, choose <cfg> ex. Debug, Release, MinSizeRel, RelWithDebInfo

https://cmake.org/cmake/help/v2.8.11/cmake.html#opt%3a--builddir

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文