(CMake) 指定生成器 generator

news/2023/12/5 23:57:28

文章目录

  • 问题引入
  • 具体处理
    • 当前环境
    • 例子
    • 命令行
    • 设置变量
  • END 附录
    • win cmake 3.24.2 help
    • linux cmake 3.10.2 help

cmake基础:

(CMake) 从下载到构建第一个CMake应用_cmake编译qt项目_天赐细莲的博客-CSDN博客

问题引入

在使用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__);
#endifreturn 0;
}

CMakelists.txt

cmake_minimum_required(VERSION 3.15)project(myCmakeDemoLANGUAGES CVERSION 1.0.0
)set(CMAKE_C_STANDARD 99)set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)set(SRC_FILESmain.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(myCmakeDemoLANGUAGES CVERSION 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_FILESmain.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
*************************

END 附录

win cmake 3.24.2 help

$ cmake --version
cmake version 3.24.2CMake suite maintained and supported by Kitware (kitware.com/cmake).$ cmake --help
Usagecmake [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 bygenerator.-A <platform-name>           = Specify platform name if supported bygenerator.--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 warningserrors.-Wno-error=deprecated        = Make deprecated macro and function warningsnot 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 anyexisting cache file.--build <dir>                = Build a CMake-generated project binary tree.--install <dir>              = Install a CMake-generated project binarytree.--open <dir>                 = Open generated project in the associatedapplication.-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, seeCMakeGraphVizOptions.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 CMakefiles.  --loglevel is also accepted forbackward 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 thecomma-separated list of packages--debug-find-var=<var-name>[,...]= Limit cmake debug-find to thecomma-separated list of result variables--trace                      = Put cmake in trace mode.--trace-expand               = Put cmake in trace mode with variableexpansion.--trace-format=<human|json-v1>= Set the output format of the trace.--trace-source=<file>        = Trace only this CMake file/module.  Multipleoptions allowed.--trace-redirect=<file>      = Redirect trace output to a file instead ofstderr.--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 systemfiles.--compile-no-warning-as-error= Ignore COMPILE_WARNING_AS_ERROR property andCMAKE_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 dataenabled 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 andexit.--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.GeneratorsThe 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 2010project 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 withmingw32-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.2CMake suite maintained and supported by Kitware (kitware.com/cmake).$ cmake --help
Usagecmake [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 bygenerator.-A <platform-name>           = Specify platform name if supported bygenerator.-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 warningserrors.-Wno-error=deprecated        = Make deprecated macro and function warningsnot 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, seeCMakeGraphVizOptions.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 variableexpansion.--trace-source=<file>        = Trace only this CMake file/module.  Multipleoptions 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 systemfiles.--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 andexit.--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.GeneratorsThe 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.

http://www.ppmy.cn/news/1027191.html

相关文章

LabVIEW控制通用工作台

LabVIEW控制通用工作台 用于教育目的的计算机化实验室显着增长&#xff0c;特别是用于运动控制的实验室。它们代表了各种工业应用中不断扩大的领域&#xff0c;并成为以安全的方式使用通常昂贵或独特的实验室设备进行实时实验的宝贵工具。NI LabVIEW等软件应用程序的开发和不断…

【torch.nn.PixelShuffle】和 【torch.nn.UnpixelShuffle】

文章目录 torch.nn.PixelShuffle直观解释官方文档 torch.nn.PixelUnshuffle直观解释官方文档 torch.nn.PixelShuffle 直观解释 PixelShuffle是一种上采样方法&#xff0c;它将形状为 ( ∗ , C r 2 , H , W ) (∗, C\times r^2, H, W) (∗,Cr2,H,W)的张量重新排列转换为形状为…

系统架构设计专业技能 · 网络规划与设计(三)【系统架构设计师】

系列文章目录 系统架构设计专业技能 网络规划与设计&#xff08;三&#xff09;【系统架构设计师】 系统架构设计专业技能 系统安全分析与设计&#xff08;四&#xff09;【系统架构设计师】 系统架构设计高级技能 软件架构设计&#xff08;一&#xff09;【系统架构设计师…

linux添加磁盘

一、linux虚拟机添加一块新的硬盘 四步&#xff1a; &#xff08;1&#xff09; &#xff08;2&#xff09;为硬盘进行分区 &#xff08;3&#xff09;初始化硬盘分区 &#xff08;4&#xff09;挂载 在虚拟机上添加一块硬盘 (1)、 虚拟机添加一块新的硬盘作为数据盘 (2) ls…

笙默考试管理系统-MyExamTest----codemirror(6)

笙默考试管理系统-MyExamTest----codemirror&#xff08;6&#xff09; 目录 笙默考试管理系统-MyExamTest----codemirror&#xff08;6&#xff09; 一、 笙默考试管理系统-MyExamTest----codemirror 二、 笙默考试管理系统-MyExamTest----codemirror 三、 笙默考试管理…

山东布谷科技直播程序源码使用Redis进行服务器横向扩展

当今&#xff0c;直播程序源码平台作为新媒体时代主流&#xff0c;受到了世界各地人民的喜爱&#xff0c;这也使得直播程序源码平台用户数量的庞大&#xff0c;也难免会出现大量用户同时访问服务器&#xff0c;使服务器过载的情况&#xff0c;当服务器承受不住的时候&#xff0…

阿里云服务器搭建Magento电子商务网站图文教程

本文阿里云百科分享使用阿里云服务器手动搭建Magento电子商务网站全流程&#xff0c;Magento是一款开源电商网站框架&#xff0c;其丰富的模块化架构体系及拓展功能可为大中型站点提供解决方案。Magento使用PHP开发&#xff0c;支持版本范围从PHP 5.6到PHP 7.1&#xff0c;并使…

C++隐式调用和explicit关键字

隐式类型转换 #include <iostream> using namespace std;class Point { public:int x, y;Point(int x 0, int y 0): x(x), y(y) {} };void displayPoint(const Point& p) {cout << "(" << p.x << "," << p.y <&l…

Linux 终端命令之文件浏览(1) cat

Linux 文件浏览命令 cat, more, less, head, tail&#xff0c;此五个文件浏览类的命令皆为外部命令。 hannHannYang:~$ which cat /usr/bin/cat hannHannYang:~$ which more /usr/bin/more hannHannYang:~$ which less /usr/bin/less hannHannYang:~$ which head /usr/bin/he…

做视频_Style

Video 1> 风格2> 技巧3> 借鉴 &#x1f517; B站视频 1> 风格 记录分享生活&#xff0c;工作&#xff0c;学习方面的总结&#xff1b; 4个段位&#xff1a; 实用 -> 简洁 -> 清晰流畅 -> 生动有趣 2> 技巧 1> 大视频分段录制&#xff0c;最后合并…

Android FrameWork 层 Handler源码解析

Handler生产者-消费者模型 在android开发中&#xff0c;经常会在子线程中进行一些耗时操作&#xff0c;当操作完毕后会通过handler发送一些数据给主线程&#xff0c;通知主线程做相应的操作。 其中&#xff1a;子线程、handler、主线程&#xff0c;其实构成了线程模型中经典的…

从Spring源码看Spring如何解决循环引用的问题

Spring如何解决循环引用的问题 关于循环引用&#xff0c;首先说一个结论&#xff1a; Spring能够解决的情况为&#xff1a;两个对象都是单实例、且通过set方法进行注入。 两个对象都是单实例&#xff0c;通过构造方法进行注入&#xff0c;Spring不能进行循环引用问题&#x…

基于k8s job设计与实现CI/CD系统

方案一&#xff1a;Jenkinsk8sCICD 方案二&#xff1a;kanikok8s jobCICD CICD 基于K8s Job设计流水线 CI方案 工具镜像 云原生镜像打包工具 kaniko的使用 与Jenkins对比 可用性与易用性

线性回归算法是什么

目录 线性回归是什么 线性回归方程 实现预测的流程 1) 数据采集 2) 构建线性回归模型

redis 数据结构(一)

Redis 为什么那么快 redis是一种内存数据库&#xff0c;所有的操作都是在内存中进行的&#xff0c;还有一种重要原因是&#xff1a;它的数据结构的设计对数据进行增删查改操作很高效。 redis的数据结构是什么 redis数据结构是对redis键值对值的数据类型的底层的实现&#xff0c…

微信小程序在使用vant组件库时构建npm报错

在跟着vant官方进行使用步骤一步步操作时&#xff0c;由于要构建NPM&#xff0c;但NPM包在App配置文件的外部 所以在做下图这一步时&#xff1a; 接着再进行npm构建时会报错 message:发生错误 Error: F:\前端学习\前端框架\小程序\project\demo\miniprogram解决方法 &#xf…

【第二阶段】it关键字

1.invoke&#xff08;&#xff09;函数 meThod1(1,2,3)等价meThod1.invoke(1,2,3) fun main() {val meThod1:(Int,Int,Int)->String{n1,n2,n3->val num"kotlin"println("num$num,n1$n1,n2$n2,,n3$n3")"num$num,n1$n1,n2$n2,,n3$n3,"}//调…

【Java数据结构】第九章 —— 树和二叉树

导航小助手 一、树 1.1 什么是树 1.2 相关概念 1.3 树的表现形式 二、二叉树 2.1 二叉树的概念 2.2 两种特殊的二叉树 2.2.1 满二叉树 2.2.2 完全二叉树 2.3 二叉树的性质 2.4 二叉树性质相关习题 2.5 二叉树的存储 2.6 二叉树的基本操作 2.6.1 前置说明 2.6.2 …

工作日(day)

庐阳区2022年信息学竞赛试题 题目描述 Description 目前我国实行五天工作制&#xff0c;即周一到周五为工作日&#xff0c;周六和周日为休息日&#xff0c;假设今天是星期 X,我们想知道从今天开始的连续N天中有多少工作日&#xff0c;不考虑节假日。 输入描述 Input Descript…

【leetcode】前缀和

内容摘抄自&#xff1a; 小而美的算法技巧&#xff1a;前缀和数组 | labuladong 的算法小抄 一维数组的前缀和 看这个 preSum 数组&#xff0c;若想求索引区间 [1, 4] 内的所有元素之和&#xff0c; 就可以通过 preSum[5] - preSum[1] 得出。 class NumArray {private:// 前缀…
最新文章