本文翻译自: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 withgcc
? 我如何表达将使用g++
编译主可执行文件以及使用gcc
编译一个嵌套库?
#1楼
参考:https://stackoom.com/question/WPVp/CMake中的调试与发布
#2楼
// 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)
#3楼
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")
顺便说一句),你可以使用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. 这会将指定的警告添加到所有构建类型,但是仅将给定的调试标志添加到DEBUG
构建。 Note that compile options are stored as a CMake list, which is just a string separating its elements by semicolons ;
请注意,编译选项存储为CMake列表,它只是一个用分号将其元素分隔开的字符串;
. 。
#4楼
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
#5楼
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. 它需要类似Release
, Debug
等的值。
http://cmake.org/Wiki/CMake_Useful_Variables#Compilers_and_Tools http://cmake.org/Wiki/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 ++选择特定的编译器。
#6楼
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. 还有RelWithDebInfo
和MinSizeRel
构建配置。
You can modify/add to the flags by specifying a toolchain file in which you can add CMAKE_C_FLAGS_DEBUG
and CMAKE_C_FLAGS_RELEASE
variables, eg: 您可以通过指定工具链文件来修改/添加到标志,可以在其中添加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_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应该自动检测并使用适合于您的不同源文件的编译器。