使用 cmake,您如何确定每个构建类型的默认编译器标志? [英] Using cmake, how do you determine the default compiler flags per build type?

查看:25
本文介绍了使用 cmake,您如何确定每个构建类型的默认编译器标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试配置我的构建系统,以便我的发布版本不包含额外的调试.但是我在 CMake 文档中找不到任何列出每个构建类型默认使用哪些编译器标志的地方,我宁愿不发明自己的做事方式.

I'm trying to configure my build system so that my release builds don't have extra debugging included. But I can't find anywhere in the CMake documentation that lists which compiler flags are used by default per build type, and I'd rather not invent my own way of doing things.

此功能是否已经存在?在不诉诸试验和错误的情况下,我如何确定默认情况下为各种构建类型使用哪些标志?

Does this functionality already exist? Without resorting to trial and error, how can I determine what flags are used by default for the various build types?

推荐答案

这篇博文 有一些有用的信息,以及这篇文章 描述了一些常见的反模式.

This blog post has some helpful information, and this post describes some common anti-patterns.

CMake 开箱即用的四种构建类型是 ReleaseDebugRelWithDebInfoMinSizeRel.相应地,CMake 在 CMAKE_C_FLAGS_CMAKE_CXX_FLAGS_ 中为每个定义的构建类型提供了默认值.

The four build types that CMake includes out of the box are Release, Debug, RelWithDebInfo, and MinSizeRel. Correspondingly, CMake ships with default values for each defined build type in CMAKE_C_FLAGS_<buildType> and CMAKE_CXX_FLAGS_<buildType>.

如果您想了解每种构建类型的默认值,您可以将以下语句添加到您的 CMakeLists.txt 中:

If you want to find out the what the default values are for each build type, you can add the following statements to your CMakeLists.txt:

message("CMAKE_C_FLAGS_DEBUG is ${CMAKE_C_FLAGS_DEBUG}")
message("CMAKE_C_FLAGS_RELEASE is ${CMAKE_C_FLAGS_RELEASE}")
message("CMAKE_C_FLAGS_RELWITHDEBINFO is ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
message("CMAKE_C_FLAGS_MINSIZEREL is ${CMAKE_C_FLAGS_MINSIZEREL}")

message("CMAKE_CXX_FLAGS_DEBUG is ${CMAKE_CXX_FLAGS_DEBUG}")
message("CMAKE_CXX_FLAGS_RELEASE is ${CMAKE_CXX_FLAGS_RELEASE}")
message("CMAKE_CXX_FLAGS_RELWITHDEBINFO is ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
message("CMAKE_CXX_FLAGS_MINSIZEREL is ${CMAKE_CXX_FLAGS_MINSIZEREL}")

在我的 cmake 版本(OS X 上的 cmake 版本 2.8.12.1)上,这会打印以下值:

On my version of cmake (cmake version 2.8.12.1 on OS X), this prints the following values:

CMAKE_C_FLAGS_DEBUG is -g
CMAKE_C_FLAGS_RELEASE is -O3 -DNDEBUG
CMAKE_C_FLAGS_RELWITHDEBINFO is -O2 -g -DNDEBUG
CMAKE_C_FLAGS_MINSIZEREL is -Os -DNDEBUG

CMAKE_CXX_FLAGS_DEBUG is -g
CMAKE_CXX_FLAGS_RELEASE is -O3 -DNDEBUG
CMAKE_CXX_FLAGS_RELWITHDEBINFO is -O2 -g -DNDEBUG
CMAKE_CXX_FLAGS_MINSIZEREL is -Os -DNDEBUG

(如您所见,默认情况下,C 和 C++ 的标志集是相同的.)

(as you can see, the sets of flags are the same for C and C++ by default.)

小心,因为默认构建类型是空字符串.因此,除非您指定构建类型,否则以上都不适用.在 cmake 邮件列表上,建议使用以下代码(应该被放置在顶级 CMakeLists.txt 的顶部附近,我想)对于那些不希望这种行为的人:

Be cautious, because the default build type is an empty string. So unless you specify a build type, none of the above applies. On the cmake mailing list, the following code was suggested (should be placed near the top of a top-level CMakeLists.txt, I imagine) for those who do not desire this behavior:

if (NOT CMAKE_BUILD_TYPE)
    message(STATUS "No build type selected, default to Release")
    set(CMAKE_BUILD_TYPE "Release")
endif()

然而,这是不推荐,因为它会破坏一些多配置生成器.(最好在构建脚本中设置它.)

However, this is not recommended, because it will break some mutli-configuration generators. (Better to set it inside a build script.)

(上面的一篇博文提倡在第一次调用 cmake 时使用 shell 别名来设置 -DCMAKE_BUILD_TYPE=Debug.)

(One of the blog posts above advocated using a shell alias to set -DCMAKE_BUILD_TYPE=Debug when cmake is first invoked instead.)

这篇关于使用 cmake,您如何确定每个构建类型的默认编译器标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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