C++ CMake 中的调试与发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7724569/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Debug vs Release in CMake
提问by Cartesius00
In a GCC compiled project,
在 GCC 编译的项目中,
- 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 withgcc
?
- 如何为每个目标类型(调试/发布)运行 CMake?
- 如何使用 CMake 指定调试和发布 C/C++ 标志?
- 我如何表示主可执行文件将
g++
与一个嵌套库一起编译gcc
?
回答by kb1ooo
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:
使用 CMake,通常建议进行“源外”构建。CMakeLists.txt
在项目的根目录中创建您的。然后从您的项目的根目录:
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. There are also RelWithDebInfo
and MinSizeRel
build configurations.
Release
/Debug
将为您的编译器添加适当的标志。还有RelWithDebInfo
和MinSizeRel
构建配置。
You can modify/add to the flags by specifying a toolchain filein which you can add CMAKE_C_FLAGS_DEBUG
and CMAKE_C_FLAGS_RELEASE
variables, e.g.:
您可以修改/通过指定添加到该标志的工具链文件,您可以在其中添加CMAKE_C_FLAGS_DEBUG
和CMAKE_C_FLAGS_RELEASE
变量,例如:
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wall")
See CMAKE_BUILD_TYPEfor 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 应该自动检测并使用适合您不同源文件的编译器。
回答by duncan
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.
对于调试/发布标志,请参阅CMAKE_BUILD_TYPE
变量(您将其作为 传递cmake -DCMAKE_BUILD_TYPE=value
)。这需要像值Release
,Debug
等等。
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++. The link above also shows how to select a specific compiler for C/C++.
将使用 g++ 编译 .c 文件。上面的链接还显示了如何为 C/C++ 选择特定的编译器。
回答by sebastian
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_compiler_options
:
相反操纵的CMAKE_CXX_FLAGS
直接串(可以更很好地使用完成string(APPEND CMAKE_CXX_FLAGS_DEBUG " -g3")
,你可以使用BTW) add_compiler_options
:
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. Note that compile options are stored as a CMake list, which is just a string separating its elements by semicolons ;
.
这会将指定的警告添加到所有构建类型,但仅将给定的调试标志添加到DEBUG
构建中。请注意,编译选项存储为 CMake 列表,它只是一个用分号分隔其元素的字符串;
。
回答by Russel Waters
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
如果你想构建一个不同的配置而不重新生成如果使用你也可以运行cmake --build {$PWD} --config <cfg>
对于多配置工具,选择<cfg>
ex。调试、发布、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
回答by sailfish009
// 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)