阅读 63

如何用cmake编译

CMake编译原理

CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多。CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt文件转化为make所需要的makefile文件,最后用make命令编译源码生成可执行程序或共享库(so(shared object))。因此CMake的编译基本就两个步骤:

  1. cmake
  2. make

compile.sh

g++ -rdynamic ../include/incl/tfc_base_config_file.cpp ../include/mq/*.cpp local_util.cpp AgentMemRpt.cpp AgentDiskRpt.cpp AgentLoadRpt.cpp AgentIoRpt.cpp AgentNetRpt.cpp AgentCpuRpt.cpp AgentProcessRpt.cpp AgentParentRpt.cpp AgentSysTop_5.cpp BaseFeatureRptMain.cpp -o rpt_main -I../include/incl -I../include/mq -I../include/rapidjson -lpthread -ldl
复制代码

CMake说明

一般把CMakeLists.txt文件放在工程目录下,使用时,先创建一个叫build的文件夹(这个并非必须,因为cmake命令指向CMakeLists.txt所在的目录,例如cmake .. 表示CMakeLists.txt在当前目录的上一级目录。cmake后会生成很多编译的中间文件以及makefile文件,所以一般建议新建一个新的目录,专门用来编译),然后执行下列操作:

cd build 
cmake .. 
make 
复制代码

其中cmake .. 在build里生成Makefile,make根据生成makefile文件,编译程序,make应当在有Makefile的目录下,根据Makefile生成可执行文件。

编写 CMakeList.txt


# 1. 声明要求的cmake最低版本
cmake_minimum_required( VERSION 2.8 )

# 2. 添加c++11标准支持
#set( CMAKE_CXX_FLAGS "-std=c++11" )

# 3. 声明一个cmake工程
PROJECT(rpt_main)

MESSAGE(STATUS "Project: SERVER") #打印相关消息消息
  
# 4. 头文件
include_directories(
${PROJECT_SOURCE_DIR}/../include/mq 
${PROJECT_SOURCE_DIR}/../include/incl 
${PROJECT_SOURCE_DIR}/../include/rapidjson
)

# 5. 通过设定SRC变量,将源代码路径都给SRC,如果有多个,可以直接在后面继续添加
set(SRC 
${PROJECT_SOURCE_DIR}/../include/incl/tfc_base_config_file.cpp 
${PROJECT_SOURCE_DIR}/../include/mq/tfc_ipc_sv.cpp 
${PROJECT_SOURCE_DIR}/../include/mq/tfc_net_ipc_mq.cpp
${PROJECT_SOURCE_DIR}/../include/mq/tfc_net_open_mq.cpp 
${PROJECT_SOURCE_DIR}/local_util.cpp
${PROJECT_SOURCE_DIR}/AgentMemRpt.cpp 
${PROJECT_SOURCE_DIR}/AgentDiskRpt.cpp 
${PROJECT_SOURCE_DIR}/AgentLoadRpt.cpp 
${PROJECT_SOURCE_DIR}/AgentIoRpt.cpp
${PROJECT_SOURCE_DIR}/AgentNetRpt.cpp 
${PROJECT_SOURCE_DIR}/AgentCpuRpt.cpp 
${PROJECT_SOURCE_DIR}/AgentProcessRpt.cpp 
${PROJECT_SOURCE_DIR}/AgentParentRpt.cpp
${PROJECT_SOURCE_DIR}/AgentSysTop_5.cpp 
${PROJECT_SOURCE_DIR}/BaseFeatureRptMain.cpp 
)

# 6. 创建共享库/静态库

# 设置路径(下面生成共享库的路径)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
# 即生成的共享库在工程文件夹下的lib文件夹中
 
set(LIB_NAME rpt_main_lib)
# 创建共享库(把工程内的cpp文件都创建成共享库文件,方便通过头文件来调用)
# 这时候只需要cpp,不需要有主函数 
# ${PROJECT_NAME}是生成的库名 表示生成的共享库文件就叫做 lib工程名.so
# 也可以专门写cmakelists来编译一个没有主函数的程序来生成共享库,供其它程序使用
# SHARED为生成动态库,STATIC为生成静态库
add_library(${LIB_NAME} STATIC ${SRC})
 
# 7. 链接库文件
# 把刚刚生成的${LIB_NAME}库和所需的其它库链接起来
# 如果需要链接其他的动态库,-l后接去除lib前缀和.so后缀的名称,以链接
# libpthread.so 为例,-lpthread
target_link_libraries(${LIB_NAME} pthread dl)
   
# 8. 编译主函数,生成可执行文件
# 先设置路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/bin)
   
# 可执行文件生成
add_executable(${PROJECT_NAME} ${SRC})
   
# 这个可执行文件所需的库(一般就是刚刚生成的工程的库咯)
target_link_libraries(${PROJECT_NAME} pthread dl ${LIB_NAME})


复制代码

使用 cmake

  • 进入/home/pzqu/agent/libvirt_base_feature/build目录
  • 执行命令 cmake ..

cmake ..

  • 查看生成的目录结构,此目录结构是中间代码,不用提交到git
[root@TJSJZVM000456 /home/pzqu/agent/libvirt_base_feature/build]# tree
.
|-- CMakeCache.txt
|-- CMakeFiles
| |-- 2.8.12.2
| | |-- CMakeCCompiler.cmake
| | |-- CMakeCXXCompiler.cmake
| | |-- CMakeDetermineCompilerABI_C.bin
| | |-- CMakeDetermineCompilerABI_CXX.bin
| | |-- CMakeSystem.cmake
| | |-- CompilerIdC
| | | |-- CMakeCCompilerId.c
| | | `-- a.out
| | `-- CompilerIdCXX
| | |-- CMakeCXXCompilerId.cpp
| | `-- a.out
| |-- CMakeDirectoryInformation.cmake
| |-- CMakeOutput.log
| |-- CMakeTmp
| |-- Makefile.cmake
| |-- Makefile2
| |-- TargetDirectories.txt
| |-- cmake.check_cache
| |-- progress.marks
| |-- rpt_main.dir
| | |-- DependInfo.cmake
| | |-- build.make
| | |-- cmake_clean.cmake
| | |-- depend.make
| | |-- flags.make
| | |-- home
| | | `-- pzqu
| | | `-- agent
| | | `-- include
| | | |-- incl
| | | `-- mq
| | |-- link.txt
| | `-- progress.make
| `-- rpt_main_lib.dir
| |-- DependInfo.cmake
| |-- build.make
| |-- cmake_clean.cmake
| |-- cmake_clean_target.cmake
| |-- depend.make
| |-- flags.make
| |-- home
| | `-- pzqu
| | `-- agent
| | `-- include
| | |-- incl
| | `-- mq
| |-- link.txt
| `-- progress.make
|-- Makefile
`-- cmake_install.cmake
复制代码
  • 使用make命令编译得到二进制文件

make

  • 二进制文件所在目录(CMakeLists.txt文件配置)

成功生成二进制文件

下次教大家如何用Clion自动同步代码到服务器上,并进行debug

关注下面的标签,发现更多相似文章
评论