1. cmake-流程和使用

本文详细介绍了CMake的基本流程,包括配置CMakeLists.txt,使用命令行、cmake-gui和ccmake进行项目构建。内容涵盖Unix、Windows以及使用Ninja等构建工具的不同平台下CMake的运行方式。此外,还提到了CMake与其他IDE(如Xcode、Qt Creator)的集成以及如何查看和选择平台下的可用生成器。
摘要由CSDN通过智能技术生成

cmake基本流程

  • 配置:在CMakeLists.txt编写规则(编译规则(预处理 编译 汇编 链接), 安装(部署)规则, 测试规则)。
  • 生成:根据CMakeLists.txt 和 参数(使用cmake时传入的参数或者环境变量参数) 针对不同平台(-G <generator>) 生成对应的编译文件(例如 Linux下的Makefile, Windows下的 *.sln, Mac OS 下的 *.xcodeproj)

还需要使用特定平台的编译器(make vs) 构建项目(包括: 预处理 编译(*.i) 汇编(*.o) 链接 测试 部署 等等) 不过这不属于cmake的职责了

运行方式

Command Line

cmake <srccode_dir> --build <build_dir> -G <generator>
- 源码路径()必须指定
- build路径()默认是当前路径”.”
- 生成器 如果不指定测试用默认值,如 linux下Unix Makefiles windows下Visual Studio 12 2013 Win64

常用方式:

mkdir build
cd build
cmake ..

cmake-gui

配置源码路径(等价于命令行中 ) 以及build路径(等价于命令行中)

配置生成器选项(等价于命令行 -G )

After the initial configure step, the GUI will show you a list of cache variables, similar to the list you see when you run cmake -L -N . from the command line.New cache variables are highlighted in red. (In this case, that’s all of them.) If you click Configure again, the red highlights will disappear, since the variables are no longer considered new.

Once you’ve customized the cache variables to your liking, click Generate. This will generate the build pipeline in the binary folder. You can then use it to build your project.

ccmake

ccmake is the console equivalent to cmake-gui. Like the GUI, it lets you set cache variables interactively. It can be handy when running CMake on a remote machine, or if you just like using the console

各个平台下运行

unix (Unix Makefiles)

CMake generates a Unix makefile by default when run from the command line in a Unix-like environment. Of course, you can generate makefiles explicitly using the -G option. When generating a makefile, you should also define the CMAKE_BUILD_TYPE variable. Assuming the source folder is the parent:

cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Debug ..

You should define the CMAKE_BUILD_TYPE variable because makefiles generated by CMake are single-configuration. Unlike a Visual Studio solution, you can’t use the same makefile to build multiple configurations such as Debug and Release. A single makefile is capable of building exactly one build type. By default, the available types are Debug, MinSizeRel, RelWithDebInfo and Release. Watch out – if you forget to define CMAKE_BUILD_TYPE, you’ll probably get an unoptimized build without debug information, which is useless. To change to a different build type, you must re-run CMake and generate a new makefile.

Once the makefile exists, you can actually build your project by running make. By default, make will build every target that was defined by CMakeLists.txt

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值