CMake简介
使用简单方便,可以跨平台,构建项目编译环境。尤其比直接写Makefile简单(在构建大型工程编译时,需要写大量的文件依赖关系),可以通过简单的CMake生成负责的Makefile文件。
CMake安装
ubuntu上直接执行 sudo apt install cmake 安装完成,可以通过cmake -version查看其版本:
CMake使用介绍
cmake
命令会执行目录下的CMakeLists.txt
配置文件里面的配置项,一个基本的CMakeLists.txt的配置文件内容如下:
cmake_minimum_required (VERSION 2.8) #要求cmake最低的版本号
project (demo) # 定义当前工程名字
# 设置debug模式,如果没有这一行将不能调试设断点
set(CMAKE_BUILD_TYPE "Debug")
# 有了这个编译文件就可以进行调试了 可以是-g -O2 等
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)
# 添加可执行文件 和 生成可执行文件的依赖项(main.c)
add_executable(main main.c)
# 进入子目录下执行 CMakeLists.txt文件 这里的lib和tests里面都有可编译的代码文件
add_subdirectory(lib)
add_subdirectory(tests)
示例一
cmake_minimum_required (VERSION 2.8) #要求cmake最低的版本号
project (demo) # 定义当前工程名字
set(CMAKE_BUILD_TYPE "Debug") #设置debug模式
add_executable(main main.cpp) #生成可执行文件main
保存退出,执行cmake .
命令,输出如下:
tony@tony-virtual-machine:~/code/cmake/rpc$ cmake .
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for work