文章目录

  • CMake简介
  • CMake的安装
  • CMake的使用
  • CMake常用的预定义变量


CMake简介

使用简单方便,可以跨平台,构建项目编译环境。尤其比直接写Makefile简单(在构建大型工程编译时,需要写大量的文件依赖关系),可以通过简单的CMake生成负责的Makefile文件。

CMake的安装

ubantu上直接执行sudo apt install cmake,安装完成,可以通过cmake -version查看其版本:

dzg@dzg-virtual-machine:~/桌面/test$ cmake -version
cmake version 3.10.2

CMake suite maintained and supported by Kitware (kitware.com/cmake).

CMake的使用

cmake命令会执行目录下的CMakeLists.txt配置文件里面的配置项,一个基本的CMakeLists.txt的配置文件内容如下:

cmake_minimum_required(VERSION 3.0)   #要求cmake最低的版本号 
project(main)  # 定义当前工程名字


#set(CMAKE_BUILD_TYPE "Debug")#设置debug模式,如果没有这一行将不能调试设断点

#配置编译选项
set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} -g)
#配置头文件搜索路径
#include_directories()
#配置库文件搜索路径
#link_directories()

#设置需要编译的源文件
set(SRC_LIST ./muduo_server.cpp)

# 设置cmake的全局变量
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

#把.指定路径下的所有源文件名字放入变量名SRC_LIST里面
#aux_source_directory(. SRC_LIST)

#生成可执行文件,由SRC_LIST变量所定义的源文件编译出来
add_executable(server ${SRC_LIST})

#表示server这个目标程序,需要连接muduo_net muduo_base pthread这三个库文件  
target_link_libraries(server muduo_net muduo_base pthread)

执行cmake .在指定的目录下来根据CMakefile.txt所写的配置进行编译

dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ cmake .
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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 working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dzg/桌面/test/testmuduo

编译完成后所生成的一些文件:

dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  Makefile  muduo_server.cpp

最后执行make

dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ make
Scanning dependencies of target server
[ 50%] Building CXX object CMakeFiles/server.dir/muduo_server.cpp.o
[100%] Linking CXX executable server
[100%] Built target server

生成可执行文件 server

dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  CMakeLists.txt  Makefile  muduo_server.cpp  server

一个正式的工程构建:

一个正式的源码工程应该有这几个目录:
-bin 存放最终的可执行文件
-build 存放编译中间文件
-lib 存放编译生成的库文件

-include 头文件
–sum.h
–minor.h

-src 源代码文件
–sum.cpp
–minor.cpp
–CMakeLists.txt

-test 测试代码
main.cpp
-CMakeLists.txt

如果想将所写的代码编译规则与源码编译规则差不多,则可以使用如下方式:

dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ ls
bin  CMakeLists.txt  muduo_server.cpp
dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ mkdir build
dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ ls
bin  build  CMakeLists.txt  muduo_server.cpp
dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ cd build/
dzg@dzg-virtual-machine:~/桌面/test/testmuduo/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.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 working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/dzg/桌面/test/testmuduo/build
dzg@dzg-virtual-machine:~/桌面/test/testmuduo/build$ make
Scanning dependencies of target server
[ 50%] Building CXX object CMakeFiles/server.dir/muduo_server.cpp.o
[100%] Linking CXX executable ../bin/server
[100%] Built target server
dzg@dzg-virtual-machine:~/桌面/test/testmuduo/build$ cd ..
dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ ls
bin  build  CMakeLists.txt  muduo_server.cpp
dzg@dzg-virtual-machine:~/桌面/test/testmuduo$ cd bin/
dzg@dzg-virtual-machine:~/桌面/test/testmuduo/bin$ ls
server
dzg@dzg-virtual-machine:~/桌面/test/testmuduo/bin$

CMake常用的预定义变量

PROJECT_NAME : 通过 project() 指定项目名称
PROJECT_SOURCE_DIR : 工程的根目录
PROJECT_BINARY_DIR : 执行 cmake 命令的目录
CMAKE_CURRENT_SOURCE_DIR : 当前 CMakeList.txt 文件所在的目录
CMAKE_CURRENT_BINARY_DIR : 编译目录,可使用 add subdirectory 来修改
EXECUTABLE_OUTPUT_PATH : 二进制可执行文件输出位置
LIBRARY_OUTPUT_PATH : 库文件输出位置
BUILD_SHARED_LIBS : 默认的库编译方式 ( shared 或 static ) ,默认为 static
CMAKE_C_FLAGS : 设置 C 编译选项
CMAKE_CXX_FLAGS : 设置 C++ 编译选项
CMAKE_CXX_FLAGS_DEBUG : 设置编译类型 Debug 时的编译选项
CMAKE_CXX_FLAGS_RELEASE : 设置编译类型 Release 时的编译选项
CMAKE_GENERATOR : 编译器名称
CMAKE_COMMAND : CMake 可执行文件本身的全路径
CMAKE_BUILD_TYPE : 工程编译生成的版本, Debug / Release