cmake入门系列总结一
cmake入门系列总结一
版本说明
版本 | 作者 | 日期 | 备注 |
---|---|---|---|
0.1 | loon | 2019.3.11 | 初稿 |
目录
文章目录
一、cmake简介及本次系列总结的初衷
长话短说,cmake是一个跨平台的编译工具,我目前用来做c/c++应用的项目构建及编译工具,目前很多的软件都使用cmake作为项目构建工具(还有我们比较熟悉的configure系的脚本)。
我目前开发的项目的大部分模块是使用cmake编译的,加上我在之前编译MySQL源码时接触过cmake,所以这里的总结算是一个学习笔记,将官网上的辅助教程进行翻译,结合一些小例子进行学习和使用。
cmake官网:https://cmake.org
cmake官网帮助文档:https://cmake.org/documentation/
cmake入门系列总结来源:https://cmake.org/cmake-tutorial/
二、基本起点-helloworld
还是老生常谈,写一个打印helloworld的程序进行cmake编译脚本编译。
1、创建一个hello_world目录
在该目录下创建一个打印helloWorld的文件和main函数。
mkdir hello_world
cd hello_world
vim main.c
main.c:
#include <stdio.h>
int main(int argc, char** argv)
{
printf("HelloWorld cmake test!\n");
return 0;
}
2、写CMakeLists.txt文件
然后写CMakeLists.txt文件(这个文件是cmake的核心,以此将生成Makefile):
我推荐Windows下使用notepad++来写,自动补全很全:
以下是一个简单的CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
add_executable(hello_world main.c)
3、构建Makefile
未构建前的目录结构:
[email protected]:~/test/cmake_test/hello_world$ l
CMakeLists.txt main.c
直接在CMakeLists.txt的同级目录下执行:
cmake .
之后目录结构变为:
[email protected]:~/test/cmake_test/hello_world$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt main.c Makefile
可以看到生成了Makefile。
之后的编译运行:
[email protected]:~/test/cmake_test/hello_world$ make
Scanning dependencies of target hello_world
[ 50%] Building C object CMakeFiles/hello_world.dir/main.c.o
[100%] Linking C executable hello_world
[100%] Built target hello_world
[email protected]:~/test/cmake_test/hello_world$ ls
CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt hello_world main.c Makefile
[email protected]:~/test/cmake_test/hello_world$ ./hello_world
HelloWorld cmake test!
4、添加版本号和配置的头文件
我们将添加的第一个功能是为我们的可执行文件和项目提供版本号。虽然您可以在源代码中专门执行此操作,但在CMakeLists.txt文件中执行此操作可提供更大的灵活性。要添加版本号,我们修改CMakeLists.txt文件,如下所示:
#cmake最小版本需要
cmake_minimum_required(VERSION 2.8)
Project(HelloWorld)
#版本号
set(HelloWorld_VERSION_MAJOR 1)
set(HelloWorld_VERSION_MINOR 0)
#配置头文件以传递一些CMake设置
#源代码
configure_file(
"${PROJECT_SOURCE_DIR}/HelloWorldConfig.h.in"
"${PROJECT_SOURCE_DIR}/HelloWorldConfig.h"
)
#将二叉树添加到包含文件的搜索路径中
#这样我们就会找到HelloWorldConfig.h
include_directories("${PROJECT_SOURCE_DIR} ")
#添加可执行文件
add_executable(hello_world main.c)
由于配置的文件将写入二叉树,我们必须将该目录添加到路径列表中以搜索包含文件。然后,我们在源代码树中创建一个HelloWorldConfig.h.in文件,其中包含以下内容:
//HelloWorld的已配置选项和设置
#define HelloWorld_VERSION_MAJOR @[email protected]
#define HelloWorld_VERSION_MINOR @[email protected]
当CMake配置此头文件时,@[email protected]和
@[email protected]的值将替换为CMakeLists.txt文件中的值。接下来,我们修改main.c以包含配置的头文件并使用版本号。结果源代码如下所示:
#include <stdio.h>
#include "HelloWorldConfig.h"
int main()
{
fprintf(stdout, "%s版本%d.%d\n", argv[0], HelloWorld_VERSION_MAJOR, HelloWorld_VERSION_MINOR);
printf("HelloWorld cmake test!\n");
return 0;
}
之后构建、编译、运行:
[email protected]:~/test/cmake_test/hello_world$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zy/test/cmake_test/hello_world
[email protected]:~/test/cmake_test/hello_world$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zy/test/cmake_test/hello_world
Scanning dependencies of target hello_world
[email protected]:~/test/cmake_test/hello_world$ ./hello_world
./hello_world版本1.0
HelloWorld cmake test!
三、最后
从上面可以看出来将cmake放在当前目录执行后会生成很多文件,会破坏项目结构的整洁性,这种情况我们会在后面进行解决,这里先提个引子。