windows 如何在 cmake 中将构建类型更改为发布模式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19024259/
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
How to change the build type to Release mode in cmake?
提问by 123r789
I am trying to build a project in Release mode. By default it is built in debug mode. I am setting the variable CMAKE_BUILD_TYPE
to "Release" in CMakeLists.txt
. But it is still building the project in debug mode.
When I pass "Release" as the build type in the CMake command, it still does not work.
我正在尝试以发布模式构建一个项目。默认情况下,它是在调试模式下构建的。我正在将变量设置CMAKE_BUILD_TYPE
为“发布” CMakeLists.txt
。但它仍在调试模式下构建项目。当我在 CMake 命令中将“Release”作为构建类型传递时,它仍然不起作用。
The CMake command that I am using is:
我使用的 CMake 命令是:
cmake -G"Visual Studio 10" -DCMAKE_BUILD_TYPE=Release
-H"source_path" -B"Build path"
Please provide a solution if any.
如果有,请提供解决方案。
采纳答案by ixSci
Use it as you do it but in the root cmake file add the following beforethe projectkeyword
使用它,但在根 cmake 文件中,在项目关键字之前添加以下内容
SET(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)
PROJECT(MY_PROJECT)#It's here just to show where you should add it.
回答by Rok
To change the build type, on Windows, it must be done at build time:
要更改构建类型,在 Windows 上,必须在构建时完成:
cmake --build {DIR} --config Release
By default it's Debug. I'm still looking for a way of changing this default. CMAKE_BUILD_TYPE doesn't work of course, and tweaking CMAKE_CONFIGURATION_TYPES doesn't work either, obviously for the same reason, they only apply for Unix makefiles, not for Visual projects.
默认情况下它是调试。我仍在寻找更改此默认值的方法。CMAKE_BUILD_TYPE 当然不起作用,调整 CMAKE_CONFIGURATION_TYPES 也不起作用,显然出于同样的原因,它们仅适用于 Unix makefile,不适用于 Visual 项目。
回答by Maks
I checked it with Visual Studio 2015 and cmake 3.3 .
我用 Visual Studio 2015 和 cmake 3.3 检查了它。
Short answer
简答
cmake --build {BUILD_DIR_PATH} --target ALL_BUILD --config {BUILD_TYPE}
Example
例子
cmake --build . --target ALL_BUILD --config Release
Long answer
长答案
cmake -G{GENERATOR_NAME} -B{BUILD_DIR_PATH} -H{SOURCE_DIR_PATH}
cmake --build {BUILD_DIR_PATH} --target ALL_BUILD --config {BUILD_TYPE}
Example
例子
cmake -GVisual Studio 14 -Bbuild/win32/x86 -H.
cmake --build build/win32/x86 --target ALL_BUILD --config Release
Additional info
附加信息
"-G" - specifies the generator name
"-B" - specifies path to the build folder
"-H" - specifies path to the source folder
“-G” - 指定生成器名称
“-B” - 指定构建文件夹的路径
“-H” - 指定源文件夹的路径
回答by ComicSansMS
You cannot set the default build type for Visual Studio from the command line.
不能从命令行设置 Visual Studio 的默认构建类型。
CMake's Visual Studio Generators will generate the four standard profiles (Debug, RelWithDebInfo, MinSizeRel and Release) and you have to choose the one you want to build from within VS. This is because the information about the active configuration is not part of the project files generated by CMake, but part of the .suo
file generated by VS.
CMake 的 Visual Studio 生成器将生成四个标准配置文件(Debug、RelWithDebInfo、MinSizeRel 和 Release),您必须从 VS 中选择要构建的一个。这是因为有关活动配置的信息不是 CMake 生成的项目文件的一部分,而是.suo
VS 生成的文件的一部分。
If you want an automated build of a particular configuration, use MSBuild instead of VS which allows you to specify a configuration on the command line.
如果您想要特定配置的自动构建,请使用 MSBuild 而不是 VS,它允许您在命令行上指定配置。