简体   繁体   English

CMake 中的调试与发布

[英]Debug vs Release in CMake

In a GCC compiled project,在 GCC 编译的项目中,

  • How do I run CMake for each target type (debug/release)?如何为每个目标类型(调试/发布)运行 CMake?
  • How do I specify debug and release C/C++ flags using CMake?如何使用 CMake 指定调试和发布 C/C++ 标志?
  • How do I express that the main executable will be compiled with g++ and one nested library with gcc ?我如何表示主可执行文件将使用g++和一个带有gcc嵌套库进行gcc

With CMake, it's generally recommended to do an "out of source" build .使用 CMake,通常建议进行“源外”构建 Create your CMakeLists.txt in the root of your project.在项目的根目录中创建CMakeLists.txt 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):对于Debug (再次从项目的根目录):

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

Release / Debug will add the appropriate flags for your compiler. Release / Debug将为您的编译器添加适当的标志。 There are also RelWithDebInfo and MinSizeRel build configurations.还有RelWithDebInfoMinSizeRel构建配置。


You can modify/add to the flags by specifying a toolchain file in which you can add CMAKE_<LANG>_FLAGS_<CONFIG>_INIT variables, eg:您可以通过指定一个工具链文件来修改/添加到标志,您可以在其中添加CMAKE_<LANG>_FLAGS_<CONFIG>_INIT变量,例如:

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

See CMAKE_BUILD_TYPE for more details.有关更多详细信息,请参阅CMAKE_BUILD_TYPE


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. CMake 应该自动检测并使用适合您不同源文件的编译器。

For debug/release flags, see the CMAKE_BUILD_TYPE variable (you pass it as cmake -DCMAKE_BUILD_TYPE=value ).对于调试/发布标志,请参阅CMAKE_BUILD_TYPE变量(您将其作为cmake -DCMAKE_BUILD_TYPE=value传递)。 It takes values like Release , Debug , etc.它采用ReleaseDebug等值。

https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/Useful-Variables#compilers-and-tools 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. cmake 使用扩展名来选择编译器,因此只需将文件命名为 .c。

You can override this with various settings:您可以使用各种设置覆盖它:

For example:例如:

set_source_files_properties(yourfile.c LANGUAGE CXX) 

Would compile .c files with g++.将使用 g++ 编译 .c 文件。 The link above also shows how to select a specific compiler for C/C++.上面的链接还显示了如何为 C/C++ 选择特定的编译器。

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 ,而不是直接操作CMAKE_CXX_FLAGS字符串(使用string(APPEND CMAKE_CXX_FLAGS_DEBUG " -g3")可以更好地完成):

add_compile_options(
  "-Wall" "-Wpedantic" "-Wextra" "-fexceptions"
  "$<$<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.这会将指定的警告添加到所有构建类型,但仅将给定的调试标志添加到DEBUG构建。 Note that compile options are stored as a CMake list, which is just a string separating its elements by semicolons ;请注意,编译选项存储为 CMake 列表,它只是一个用分号分隔其元素的字符串; . .

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.当然,我会在 2020 年回答这个问题,所以预计事情会发生变化。


How do I run CMake for each target type (debug/release)?如何为每个目标类型(调试/发布)运行 CMake?

First off Debug/Release are called configurations in cmake (nitpick).首先,调试/发布在 cmake (nitpick) 中被称为配置。

If you are using a single configuration generator (Ninja/Unix-Makefiles)如果您使用单个配置生成器(Ninja/Unix-Makefiles)

Then you need a build folder for each configuration.然后,您需要为每个配置创建一个构建文件夹。

Like this:像这样:

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

# Actually build the binaries
cmake --build build/Debug

For multi-configuration generators it's slightly different (Ninja Multi-Config, Visual Studio)对于多配置生成器,它略有不同(Ninja Multi-Config,Visual Studio)

# Configure the build
cmake -S . -B build

# Actually build the binaries
cmake --build build --config Debug

If you are wondering why this is necessary it's because cmake isn't a build system.如果您想知道为什么这是必要的,那是因为 cmake 不是构建系统。 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.这基本上是处理支持 1 个构建中的多个配置的构建系统的结果。 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如果您想更深入地了解,我建议您阅读 Craig Scott 的书“Professional CMake: A Practical Guide”中有关 cmake 的一些内容


How do I specify debug and release C/C++ flags using CMake?如何使用 CMake 指定调试和发布 C/C++ 标志?

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
    $<$<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!使用 CMAKE_BUILD_TYPE 将导致任何多配置生成器的构建错误!

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.使用 add_compile_definitions、add_compile_options 等。这些函数支持生成器表达式。 Don't use old style cmake unless you have to (that path is a land of nightmares)除非必须,否则不要使用旧式 cmake(这条路是噩梦之地)


How do I express that the main executable will be compiled with g++ and one nested library with gcc?我如何表示主可执行文件将使用 g++ 和一个带有 gcc 的嵌套库进行编译?

Your last question really doesn't make sense.你最后一个问题真的没有意义。

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.如果你想在不重新生成的情况下构建不同的配置,如果使用你也可以运行cmake --build {$PWD} --config <cfg>对于多配置工具,选择<cfg> ex。 Debug, Release, MinSizeRel, RelWithDebInfo调试、发布、MinSizeRel、RelWithDebInfo

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

// CMakeLists.txt : release // CMakeLists.txt : 发布

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

// CMakeLists.txt : debug // CMakeLists.txt : 调试

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM