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