(CMake) 指定生成器 generator
但是cmake在不指定的情况下,会使用默认的生成器,无论当前环境是否拥有该生成器。在使用cmake的时候,往往想要使用自己需要的生成器。暂时未找到修改default的方法。如何处理该情况就是本文的内容。在3.15版本开始,支持设定。此方法仅对每次操作有效。cmake -G "MinGW Makefiles"set(CMAKE_GENERATOR "MinGW Makefiles")CMAKE_GE
文章共4,985字 · 阅读需要大约17分钟
一键AI生成摘要,助你高效阅读
问答
·
cmake基础:
问题引入
在使用cmake的时候,往往想要使用自己需要的生成器。
但是cmake在不指定的情况下,会使用默认的生成器,无论当前环境是否拥有该生成器。
如何处理该情况就是本文的内容。
具体处理
当前环境
# win 10
# cmake version 3.24.2
# default Generator * Visual Studio 16 2019
例子
E:.
│ CMakelists.txt
│ main.c
│
└─build
main.c
#include <stdio.h>
int main(int argc, const char** argv) {
for (int i = 0; i < argc; i += 1) {
printf("%s\n", argv[i]);
}
#ifdef __STDC_VERSION__
printf("%ld\n", __STDC_VERSION__);
#endif
return 0;
}
CMakelists.txt
cmake_minimum_required(VERSION 3.15)
project(myCmakeDemo
LANGUAGES C
VERSION 1.0.0
)
set(CMAKE_C_STANDARD 99)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(SRC_FILES
main.c
)
add_executable(${PROJECT_NAME}
${SRC_FILES}
)
解决方案
命令行
此方法仅对每次操作有效。
暂时未找到修改default的方法
# 在`build`目录下
cmake -G "MinGW Makefiles" ../
# 或者在`CMakelists.txt` 显示指定资源路径和构建路径
# 这种方式可以自动创建`build`目录
cmake -G "MinGW Makefiles" -S . -B ./build
设置变量
在3.15版本开始,支持设定CMAKE_GENERATOR
构建器的变量
cmake_minimum_required(VERSION 3.15)
project(myCmakeDemo
LANGUAGES C
VERSION 1.0.0
)
## 核心 根据自己分支需求,指定目标的构建工具
if(MSVC)
# 3.15 才支持
set(CMAKE_GENERATOR "MinGW Makefiles")
elseif(UNIX AND NOT APPLE)
set(CMAKE_GENERATOR "Unix Makefiles")
else()
message("Waring not Win or Linux")
endif()
set(CMAKE_C_STANDARD 99)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(SRC_FILES
main.c
)
add_executable(${PROJECT_NAME}
${SRC_FILES}
)
# # debug and lod message
message("*************************")
# 项目名
message(${PROJECT_NAME})
# 指定编译器
message(${CMAKE_GENERATOR})
# 当前`CMakelists.txt`所在路径
# PROJECT_SOURCE_DIR默认效果相同,但是可配置
message(${CMAKE_CURRENT_SOURCE_DIR})
# build 构建目录
message(${CMAKE_BINARY_DIR})
# 最终程序和动态库所在路径
message(${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
message("*************************")
message效果
*************************
myCmakeDemo
MinGW Makefiles
E:/VS-Code/cmake
E:/VS-Code/cmake/build
E:/VS-Code/cmake/build/bin
*************************
设置win环境变量
在win的环境变量中进行如下设置,就可以改变默认生成器。注意重启ide,命令行等。
END 附录
win cmake 3.24.2 help
$ cmake --version
cmake version 3.24.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ cmake --help
Usage
cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
cmake [options] -S <path-to-source> -B <path-to-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.
Options
-S <path-to-source> = Explicitly specify a source directory.
-B <path-to-build> = Explicitly specify a build directory.
-C <initial-cache> = Pre-load a script to populate the cache.
-D <var>[:<type>]=<value> = Create or update a cmake cache entry.
-U <globbing_expr> = Remove matching entries from CMake cache.
-G <generator-name> = Specify a build system generator.
-T <toolset-name> = Specify toolset name if supported by
generator.
-A <platform-name> = Specify platform name if supported by
generator.
--toolchain <file> = Specify toolchain file
[CMAKE_TOOLCHAIN_FILE].
--install-prefix <directory> = Specify install directory
[CMAKE_INSTALL_PREFIX].
-Wdev = Enable developer warnings.
-Wno-dev = Suppress developer warnings.
-Werror=dev = Make developer warnings errors.
-Wno-error=dev = Make developer warnings not errors.
-Wdeprecated = Enable deprecation warnings.
-Wno-deprecated = Suppress deprecation warnings.
-Werror=deprecated = Make deprecated macro and function warnings
errors.
-Wno-error=deprecated = Make deprecated macro and function warnings
not errors.
--preset <preset>,--preset=<preset>
= Specify a configure preset.
--list-presets = List available presets.
-E = CMake command mode.
-L[A][H] = List non-advanced cached variables.
--fresh = Configure a fresh build tree, removing any
existing cache file.
--build <dir> = Build a CMake-generated project binary tree.
--install <dir> = Install a CMake-generated project binary
tree.
--open <dir> = Open generated project in the associated
application.
-N = View mode only.
-P <file> = Process script mode.
--find-package = Legacy pkg-config like mode. Do not use.
--graphviz=[file] = Generate graphviz of dependencies, see
CMakeGraphVizOptions.cmake for more.
--system-information [file] = Dump information about this system.
--log-level=<ERROR|WARNING|NOTICE|STATUS|VERBOSE|DEBUG|TRACE>
= Set the verbosity of messages from CMake
files. --loglevel is also accepted for
backward compatibility reasons.
--log-context = Prepend log messages with context, if given
--debug-trycompile = Do not delete the try_compile build tree.
Only useful on one try_compile at a time.
--debug-output = Put cmake in a debug mode.
--debug-find = Put cmake find in a debug mode.
--debug-find-pkg=<pkg-name>[,...]
= Limit cmake debug-find to the
comma-separated list of packages
--debug-find-var=<var-name>[,...]
= Limit cmake debug-find to the
comma-separated list of result variables
--trace = Put cmake in trace mode.
--trace-expand = Put cmake in trace mode with variable
expansion.
--trace-format=<human|json-v1>
= Set the output format of the trace.
--trace-source=<file> = Trace only this CMake file/module. Multiple
options allowed.
--trace-redirect=<file> = Redirect trace output to a file instead of
stderr.
--warn-uninitialized = Warn about uninitialized values.
--no-warn-unused-cli = Don't warn about command line options.
--check-system-vars = Find problems with variable usage in system
files.
--compile-no-warning-as-error= Ignore COMPILE_WARNING_AS_ERROR property and
CMAKE_COMPILE_WARNING_AS_ERROR variable.
--profiling-format=<fmt> = Output data for profiling CMake scripts.
Supported formats: google-trace
--profiling-output=<file> = Select an output path for the profiling data
enabled through --profiling-format.
--help,-help,-usage,-h,-H,/? = Print usage information and exit.
--version,-version,/V [<f>] = Print version number and exit.
--help-full [<f>] = Print all help manuals and exit.
--help-manual <man> [<f>] = Print one help manual and exit.
--help-manual-list [<f>] = List help manuals available and exit.
--help-command <cmd> [<f>] = Print help for one command and exit.
--help-command-list [<f>] = List commands with help available and exit.
--help-commands [<f>] = Print cmake-commands manual and exit.
--help-module <mod> [<f>] = Print help for one module and exit.
--help-module-list [<f>] = List modules with help available and exit.
--help-modules [<f>] = Print cmake-modules manual and exit.
--help-policy <cmp> [<f>] = Print help for one policy and exit.
--help-policy-list [<f>] = List policies with help available and exit.
--help-policies [<f>] = Print cmake-policies manual and exit.
--help-property <prop> [<f>] = Print help for one property and exit.
--help-property-list [<f>] = List properties with help available and
exit.
--help-properties [<f>] = Print cmake-properties manual and exit.
--help-variable var [<f>] = Print help for one variable and exit.
--help-variable-list [<f>] = List variables with help available and exit.
--help-variables [<f>] = Print cmake-variables manual and exit.
Generators
The following generators are available on this platform (* marks default):
Visual Studio 17 2022 = Generates Visual Studio 2022 project files.
Use -A option to specify architecture.
* Visual Studio 16 2019 = Generates Visual Studio 2019 project files.
Use -A option to specify architecture.
Visual Studio 15 2017 [arch] = Generates Visual Studio 2017 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 14 2015 [arch] = Generates Visual Studio 2015 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 12 2013 [arch] = Generates Visual Studio 2013 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 11 2012 [arch] = Generates Visual Studio 2012 project files.
Optional [arch] can be "Win64" or "ARM".
Visual Studio 10 2010 [arch] = Deprecated. Generates Visual Studio 2010
project files. Optional [arch] can be
"Win64" or "IA64".
Visual Studio 9 2008 [arch] = Generates Visual Studio 2008 project files.
Optional [arch] can be "Win64" or "IA64".
Borland Makefiles = Generates Borland makefiles.
NMake Makefiles = Generates NMake makefiles.
NMake Makefiles JOM = Generates JOM makefiles.
MSYS Makefiles = Generates MSYS makefiles.
MinGW Makefiles = Generates a make file for use with
mingw32-make.
Green Hills MULTI = Generates Green Hills MULTI files
(experimental, work-in-progress).
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Ninja Multi-Config = Generates build-<Config>.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - MinGW Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles = Generates CodeBlocks project files.
CodeBlocks - NMake Makefiles JOM
= Generates CodeBlocks project files.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - MinGW Makefiles = Generates CodeLite project files.
CodeLite - NMake Makefiles = Generates CodeLite project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Eclipse CDT4 - NMake Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - MinGW Makefiles
= Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
Kate - MinGW Makefiles = Generates Kate project files.
Kate - NMake Makefiles = Generates Kate project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Sublime Text 2 - MinGW Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - NMake Makefiles
= Generates Sublime Text 2 project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
linux cmake 3.10.2 help
$ cmake --version
cmake version 3.10.2
CMake suite maintained and supported by Kitware (kitware.com/cmake).
$ cmake --help
Usage
cmake [options] <path-to-source>
cmake [options] <path-to-existing-build>
Specify a source directory to (re-)generate a build system for it in the
current working directory. Specify an existing build directory to
re-generate its build system.
Options
-C <initial-cache> = Pre-load a script to populate the cache.
-D <var>[:<type>]=<value> = Create a cmake cache entry.
-U <globbing_expr> = Remove matching entries from CMake cache.
-G <generator-name> = Specify a build system generator.
-T <toolset-name> = Specify toolset name if supported by
generator.
-A <platform-name> = Specify platform name if supported by
generator.
-Wdev = Enable developer warnings.
-Wno-dev = Suppress developer warnings.
-Werror=dev = Make developer warnings errors.
-Wno-error=dev = Make developer warnings not errors.
-Wdeprecated = Enable deprecation warnings.
-Wno-deprecated = Suppress deprecation warnings.
-Werror=deprecated = Make deprecated macro and function warnings
errors.
-Wno-error=deprecated = Make deprecated macro and function warnings
not errors.
-E = CMake command mode.
-L[A][H] = List non-advanced cached variables.
--build <dir> = Build a CMake-generated project binary tree.
-N = View mode only.
-P <file> = Process script mode.
--find-package = Run in pkg-config like mode.
--graphviz=[file] = Generate graphviz of dependencies, see
CMakeGraphVizOptions.cmake for more.
--system-information [file] = Dump information about this system.
--debug-trycompile = Do not delete the try_compile build tree.
Only useful on one try_compile at a time.
--debug-output = Put cmake in a debug mode.
--trace = Put cmake in trace mode.
--trace-expand = Put cmake in trace mode with variable
expansion.
--trace-source=<file> = Trace only this CMake file/module. Multiple
options allowed.
--warn-uninitialized = Warn about uninitialized values.
--warn-unused-vars = Warn about unused variables.
--no-warn-unused-cli = Don't warn about command line options.
--check-system-vars = Find problems with variable usage in system
files.
--help,-help,-usage,-h,-H,/? = Print usage information and exit.
--version,-version,/V [<f>] = Print version number and exit.
--help-full [<f>] = Print all help manuals and exit.
--help-manual <man> [<f>] = Print one help manual and exit.
--help-manual-list [<f>] = List help manuals available and exit.
--help-command <cmd> [<f>] = Print help for one command and exit.
--help-command-list [<f>] = List commands with help available and exit.
--help-commands [<f>] = Print cmake-commands manual and exit.
--help-module <mod> [<f>] = Print help for one module and exit.
--help-module-list [<f>] = List modules with help available and exit.
--help-modules [<f>] = Print cmake-modules manual and exit.
--help-policy <cmp> [<f>] = Print help for one policy and exit.
--help-policy-list [<f>] = List policies with help available and exit.
--help-policies [<f>] = Print cmake-policies manual and exit.
--help-property <prop> [<f>] = Print help for one property and exit.
--help-property-list [<f>] = List properties with help available and
exit.
--help-properties [<f>] = Print cmake-properties manual and exit.
--help-variable var [<f>] = Print help for one variable and exit.
--help-variable-list [<f>] = List variables with help available and exit.
--help-variables [<f>] = Print cmake-variables manual and exit.
Generators
The following generators are available on this platform:
Unix Makefiles = Generates standard UNIX makefiles.
Ninja = Generates build.ninja files.
Watcom WMake = Generates Watcom WMake makefiles.
CodeBlocks - Ninja = Generates CodeBlocks project files.
CodeBlocks - Unix Makefiles = Generates CodeBlocks project files.
CodeLite - Ninja = Generates CodeLite project files.
CodeLite - Unix Makefiles = Generates CodeLite project files.
Sublime Text 2 - Ninja = Generates Sublime Text 2 project files.
Sublime Text 2 - Unix Makefiles
= Generates Sublime Text 2 project files.
Kate - Ninja = Generates Kate project files.
Kate - Unix Makefiles = Generates Kate project files.
Eclipse CDT4 - Ninja = Generates Eclipse CDT 4.0 project files.
Eclipse CDT4 - Unix Makefiles= Generates Eclipse CDT 4.0 project files.
KDevelop3 = Generates KDevelop 3 project files.
KDevelop3 - Unix Makefiles = Generates KDevelop 3 project files.
更多推荐
已为社区贡献2条内容
所有评论(0)