CMake是一个跨平台的安装(编译)工具,能够用简单的语句来描述全部平台的安装(编译过程)。他可以输出各类各样的makefile或者project文件,能测试编译器所支持的C++特性,相似UNIX下的automake。只是 CMake 的组态档取名为 CmakeLists.txt。Cmake 并不直接建构出最终的软件,而是产生标准的建构档(如 Unix 的 Makefile 或 Windows Visual C++ 的 projects/workspaces),而后再依通常的建构方式使用。这使得熟悉某个集成开发环境(IDE)的开发者能够用标准的方式建构他的软件,这种可使用各平台的原生建构系统的能力是 CMake 和 SCons 等其余相似系统的区别之处。linux
cmake应用c++
cmake使用除了应用程序外,就是编写CMakeLists.txt文档,以生成Makefile文件。app
1. 每一个目录下都须要文件CmakeLists.txt文件,CmakeLists.txt的编写需遵循cmake语法。ide
2. 最好在根目录下,建立build文件夹,让后进入build文件夹构建工程,这样构建工程的中间文件及最后文件都在build中,直接发布build文件便可。工具
3. 构建命令cd build; cmake ..; make;测试
注:cmake后的..是上层目录意思。ui
4. 生成可执行程序,运行便可。spa
一个最简单示例.net
//hello.c #include <stdio.h> int main() { printf("Hello World!\n"); return 0; } //CmakeLists.txt PROJECT(HELLO) SET(SRC_LIST hello.c) ADD_EXECUTABLE(hello ${SRC_LIST})
流程:命令行
~$pwd /home/yuxi/test/cmake/build ~$ls .. build CMakeLists.txt hello.c ~$cmake .. -- The C compiler identification is GNU -- The CXX compiler identification is GNU -- Check for working C compiler: /usr/bin/gcc -- Check for working C compiler: /usr/bin/gcc -- works -- Detecting C compiler ABI info -- Detecting C compiler ABI info - done -- Check for working CXX compiler: /usr/bin/c++ -- Check for working CXX compiler: /usr/bin/c++ -- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler ABI info - done -- Configuring done -- Generating done -- Build files have been written to: /home/yuxi/test/cmake/build ~$ls CMakeCache.txt CMakeFiles cmake_install.cmake Makefile ~$make Scanning dependencies of target hello [100%] Building C object CMakeFiles/hello.dir/hello.c.o Linking C executable hello [100%] Built target hello ~$ls CMakeCache.txt CMakeFiles cmake_install.cmake hello Makefile ~$./hello Hello World! ~$ls CMakeFiles/ CMakeCCompiler.cmake CMakeOutput.log Makefile2 cmake.check_cache CMakeSystem.cmake Makefile.cmake CMakeCXXCompiler.cmake CMakeTmp progress.marks CMakeDetermineCompilerABI_C.bin CompilerIdC TargetDirectories.txt CMakeDetermineCompilerABI_CXX.bin CompilerIdCXX CMakeDirectoryInformation.cmake hello.dir
Cmake debug和release设置
1. 经过命令行指定
cmake -DCMAKE_BUILD_TYPE=Release/Debug ..
2. 在CMakeLists.txt中设置(目前不起做用)
SET(CMAKE_BUILD_TYPE "Debug”) or SET(CMAKE_BUILD_TYPE "Release")
同时可在CMakeLists.txt中设置release和debug编译选项
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb") SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
参考:
1. cmake快速入门
2. CMake快速使用教程