CMake入门教程

本文详细介绍CMake在项目构建中的应用,包括基本语法、目录结构管理、动态库与静态库编译,以及如何链接库文件。通过五个实例,从简单的HelloWorld到复杂的库编译,深入浅出地讲解CMake的使用技巧。

简介

cmake的亮点在于编译复杂项目上的应用 —— cmake是一个跨平台的Makefile 生成工具! 一言以蔽之——cmake 为项目自动生成Makefile, 虽然cmake功能远不止步于此,但是本文聚焦于此。


例1:Hello World

源代码只有一个文件HelloWorld.cpp

#include<iostream>

int main(int argc, char *argv[]){
   std::cout << "Hello World!" << std::endl;
   return 0;
}

CMakeLists.txt也只有三行而已(使用cmake管理项目的过程,也就是编写CMakeLists.txt的过程)

cmake_minimum_required(VERSION 2.8.9)
project (hello)
add_executable(hello helloworld.cpp)

第一行用于指定cmake最低版本
第二行指定项目名称(这个名称是任意的)
第三行指定编译一个可执行文件,hello是第一个参数,表示生成可执行文件的文件名(这个文件名也是任意的),第二个参数helloworld.cpp则用于指定源文件。


如果您电脑上已经安装了cmake,那么我们就已经完事具备了。
第一步,用cmake生成Makefile文件

  • 注:cmake命令后边跟的就是CMakelist.txt所在的目录,这个目录不必是当前目录,你也可以新建一个build目录或者其他名字的目录来生成build文件,实际项目中也都是这么做的,这样代码会很干净也便于git管理.
    在这里插入图片描述
    第二步,make编译程序 && 编译成功
    通过上一步我们发现,当前目录下已经多出了几个文件,特别是Makefile文件
    在这里插入图片描述
    第三步,测试程序
    到此,第一个用cmake管理的程序,成功了!
    在这里插入图片描述

例2: 包含目录结构的项目

在例1中完全体现不出cmake的任何优势,用g++一行可以解决的问题我们绕了一大圈。可是cmake本来的优势就是管理庞大的项目的。
这个例子用最小的程序来体现一个带目录结构的项目。其中有源文件目录,头文件目录。
在这里插入图片描述

cmake_minimum_required(VERSION 2.8.9)
project(directory_test)

#Bring the headers, such as Student.h into the project
include_directories(include)

#Can manually add the sources using the set command as follows:
#set(SOURCES src/mainapp.cpp src/Student.cpp)

#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")

add_executable(testStudent ${SOURCES})

和第一个例子比起来,CMakelist.txt有如下改变:

  1. 使用include_directories() 包含头文件目录
  2. 使用set(SOURCES … ) 或GLOB (or GLOB_RECURSE) 设置源文件SOURCES
  3. add_executable 使用变量SOURCES ,而不是具体的文件名
    接下来的步骤就和例子1一样了,不同之处是我们新建了一个build目录来存储编译中间文件,如下图:
    在这里插入图片描述
    下一步make,然后运行结果如下:
    在这里插入图片描述

例3:动态库编译(.so)

有了前两个例子的基础,接下来的例子我们只需要看一下目录结构和CMakelist.txt.
在这里插入图片描述
CMakelist.txt如下:

project(directory_test)
set(CMAKE_BUILD_TYPE Release)

#Bring the headers, such as Student.h into the project
include_directories(include)

#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")

#Generate the shared library from the sources
add_library(testStudent SHARED ${SOURCES})

#Set the location for library installation -- i.e., /usr/lib in this case
# not really necessary in this example. Use "sudo make install" to apply
install(TARGETS testStudent DESTINATION /usr/lib)

两个重要变化:

  1. 我们不再使用add_executable() 而是使用add_library()
  2. install 指定安装目录,执行sudo make install时动态库将被安装在/usr/lib目录
    如前两个例子,我们依次执行,cmake make编译结果如下:
    在这里插入图片描述

例4:静态库编译 (.a)

基于例3,我们编译一个静态库
在这里插入图片描述
将CMakeList.txt修改为如下所示:

cmake_minimum_required(VERSION 2.8.9)
project(directory_test)
set(CMAKE_BUILD_TYPE Release)

#Bring the headers, such as Student.h into the project
include_directories(include)

#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")

#Generate the static library from the sources
add_library(testStudent STATIC ${SOURCES})

#Set the location for library installation -- i.e., /usr/lib in this case
# not really necessary in this example. Use "sudo make install" to apply
install(TARGETS testStudent DESTINATION /usr/li

可以看出,只需将add_library中的shared改为static即可。
编译结果如下:
在这里插入图片描述


例5:使用静态库或动态库

下边我们来测试一下我们例3的结果,代码和CMakeList.txt如下:

#include"Student.h"

int main(int argc, char *argv[]){
   Student s("Joe");
   s.display();
   return 0;
}
cmake_minimum_required(VERSION 2.8.9)
project (TestLibrary)

#For the shared library:
set ( PROJECT_LINK_LIBS libtestStudent.so )
link_directories( ~/exploringBB/extras/cmake/studentlib_shared/build )

#For the static library:
#set ( PROJECT_LINK_LIBS libtestStudent.a )
#link_directories( ~/exploringBB/extras/cmake/studentlib_static/build )

include_directories(~/exploringBB/extras/cmake/studentlib_shared/include)

add_executable(libtest libtest.cpp)
target_link_libraries(libtest ${PROJECT_LINK_LIBS} )

结果如下(CMakeList.txt中的目录要根据自己的情况改一下):
在这里插入图片描述
成功了!!

CMake是一个跨平台的开源构建系统,它帮助开发者在多种操作系统上自动化软件项目的构建过程。以下是CMake入门的一些步骤和基本概念: 1. **安装CMake**:首先,你需要从CMake官网下载并安装适用于你的系统的版本。对于Windows、Mac OS和Linux都有预打包的二进制包可供选择。 2. **创建CMakeLists.txt**:这是CMake的核心,它是描述你的项目结构和依赖关系的文本文件。通常放在项目根目录下。 3. **配置生成器**:CMake支持多种构建系统,如Unix Makefiles、Visual Studio等。通过命令行指定`cmake -G`选项,告诉CMake你想要哪种生成器。 4. **生成构建文件**:运行`cmake`命令初始化CMake流程,生成对应的构建文件(如Makefile或Visual Studio解决方案文件)。 5. **构建项目**:根据选定的构建系统(如make或msbuild),运行`make`或`cmake --build .`命令来实际构建你的程序。 6. **添加变量和指令**:CMake提供了许多变量和函数,如设置源代码路径、添加库依赖、设置编译选项等,可以灵活调整构建配置。 7. **了解基本语法**:学习CMake的基本语法,如IF()条件语句、ADD_EXECUTABLE()添加可执行文件、INCLUDE()导入外部CMakeLists.txt等。 8. **实践项目**:通过参与一些小型项目或者使用官方的示例来熟悉CMake的工作流程。 **相关问题--** 1. CMake有哪些常见的错误及其解决办法是什么? 2. 如何在CMakeLists.txt中处理不同平台的差异? 3. 怎样在已经存在的CMake项目中添加新的模块或依赖?
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值