目录
- [1 概述](#1 概述)
- [2 文件命名](#2 文件命名)
- [3 实例](#3 实例)
- [4 代码分析](#4 代码分析)
1 概述
编译工具有很多(make/cmake/BJam)。cmake是跨平台,使用cmake编译需要编写CMakeLists.txt。本文编写CMakeLists.txt来生成C++可执行程序。
2 文件命名
文件命名为CMakeLists.txt,是一个文本文件,可以使用任何编辑器编辑。
本文使用cmake程序版本:
bash
```bash
$cmake --version
cmake version 3.16.3
CMake suite maintained and supported by Kitware (kitware.com/cmake)
3 实例
这里以CppTest库测试代码为例,代码目录结构:
bash
test$ tree
.
├── CMakeLists.txt
├── cmdtest.cpp
├── cmdtest.h
├── inc
│ └── cpptest
│ ├── cpptest-assert.h
│ ├── cpptest-collectoroutput.h
│ ├── cpptest-compileroutput.h
│ ├── cpptest-htmloutput.h
│ ├── cpptest-output.h
│ ├── cpptest-source.h
│ ├── cpptest-suite.h
│ ├── cpptest-textoutput.h
│ ├── cpptest-time.h
│ └── cpptest.h
├── lib
│ └── libcpptest.a
└── test.cpp
3 directories, 15 files
CMakeLists.txt文件如下:
shell
cmake_minimum_required(VERSION 3.13)
set(CPPCMD_VERSION 1.0.0)
project(test VERSION ${CPPCMD_VERSION} LANGUAGES CXX)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
set(CPPCMD_LIB_DIR ../install CACHE STRING "cppcmd lib dir")
include_directories(../cppcmd/inc ./inc)
file(GLOB SRC *.cpp)
set(CMAKE_CXX_FLAGS "-std=c++11")
add_executable(${PROJECT_NAME} ${SRC})
target_link_libraries(${PROJECT_NAME} cppcmd cpptest)
target_link_directories(${PROJECT_NAME} PRIVATE lib ${CPPCMD_LIB_DIR}/lib)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
编译结果:
shell
test$ mkdir build
test$ cd build/
build$ cmake ../ -DCPPCMD_LIB_DIR=../install
-- The CXX compiler identification is GNU 10.5.0
-- 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/james/git/cppcmdv1.0.0/test/build
build$ make
Scanning dependencies of target test
[ 33%] Building CXX object CMakeFiles/test.dir/cmdtest.cpp.o
[ 66%] Building CXX object CMakeFiles/test.dir/test.cpp.o
[100%] Linking CXX executable bin/test
[100%] Built target test
说明:
- 编译生成可执行程序test文件放在build/bin目录下
- 编译生成.o放在build/CMakeFiles/test.dir/目录下
4 代码分析
shell
# 设置使用cmake的最小版本,这里最小版本是3.12
cmake_minimum_required(VERSION 3.13)
# 设置变量CPPCMD_VERSION,其值为1.0.0
set(CPPCMD_VERSION 1.0.0)
# 设置工程名称,版本,开发语言
project(test VERSION ${CPPCMD_VERSION} LANGUAGES CXX)
# 设置可执行程序输出路径为build/bin
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/bin)
# 设置cppcmd库路径
set(CPPCMD_LIB_DIR ../install CACHE STRING "cppcmd lib dir")
# 设置include路径,这里是上一级cppcmd/inc和当前路径下inc目录
include_directories(../cppcmd/inc ./inc)
# 行设置要编译的源文件,这里将当前路径下所有cpp文件名称,放在变量SRC中
file(GLOB SRC *.cpp)
# 设置定义C++编译选项,C++使用C++11标准
set(CMAKE_CXX_FLAGS "-std=c++11")
# 添加可执行目标,并设置可执行程序名称及依赖的源文件
add_executable(${PROJECT_NAME} ${SRC})
# 设置可执行程序依赖库,这里是cppcmd和cpptest
target_link_libraries(${PROJECT_NAME} cppcmd cpptest)
# 设置依赖库的搜索路径,这里是./lib和build/lib
target_link_directories(${PROJECT_NAME} PRIVATE lib ${CPPCMD_LIB_DIR}/lib)
# 安装可执行程序
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
说明:
- 变量CPPCMD_LIB_DIR为CACHE变量,默认值为.../install即源码目录的上一级目录下的install
- 可以通过命令行参数修改:
shell
cmake ../ -DCPPCMD_LIB_DIR=/usr/local/cppcmd