CMake实战二:多个源文件,同一或多个目录

简介: CMake实战一只有单个源文件,现在把add函数写入myMath.cpp的源文件里面,声明放到myMath.h源文件里面

1、同一目录,多个源文件


CMake实战一只有单个源文件,现在把add函数写入myMath.cpp的源文件里面,声明放到myMath.h源文件里面


工程树状图如下:


demo2/
├── CMakeLists.txt
├── main.cpp
├── myMath.cpp
└── myMath.h


这个时候,CMakeLists.txt 可以改成如下的形式:


# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo2)
# 指定生成目标
add_executable(Demo main.cpp myMath.cpp)
main.cpp
#include <stdio.h>
#include <stdlib.h>
#include "myMath.h"
int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("Usage: %s argv[1] argv[2] \n", argv[0]);
        return 1;
    }
    int a = atof(argv[1]);
    int b = atoi(argv[2]);
    int result = add(a, b);
    printf("%d + %d = %d\n", a, b, result);
    return 0;
}
myMath.cpp
#include "myMath.h"
int add(int a, int b) {
    return (a + b);
}
myMath.h
int add(int a, int b);


唯一的改动只是在 add_executable 命令中增加了一个 myMath.cpp 源文件。这样写当然没什么问题,但是如果源文件很多,把所有源文件的名字都加进去将是一件烦人的工作。更省事的方法是使用 aux_source_directory 命令,该命令会查找指定目录下的所有源文件,然后将结果存进指定变量名。其语法如下:


aux_source_directory(<dir> <variable>)


因此,可以修改 CMakeLists.txt 如下:


# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (demo2)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 指定生成目标
add_executable(demo ${DIR_SRCS})


这样,CMake 会将当前目录所有源文件的文件名赋值给变量 DIR_SRCS ,再指示变量 DIR_SRCS 中的源文件需要编译成一个名称为 demo 的可执行文件。


新建build目录是方便我们清理cmake产生的缓存文件,不需要的时候直接删除build目录即可


[root@hackett build]# cmake ..
-- The C compiler identification is GNU 8.4.1
-- The CXX compiler identification is GNU 8.4.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/cmake/demo2/build
[root@hackett build]# make
[ 33%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/demo.dir/myMath.cpp.o
[100%] Linking CXX executable demo
[100%] Built target demo
[root@hackett build]# ./demo 2 3
2 + 3 is 5


2、多个目录,多个源文件


myMath.hmyMath.cc 文件移动到 math 目录下


../demo3
├── CMakeLists.txt
├── main.cpp
└── math
    ├── CMakeLists.txt
    ├── myMath.cpp
    └── myMath.h


对于这种情况,需要分别在项目根目录 demo3 和 math 目录里各编写一个 CMakeLists.txt 文件。为了方便,我们可以先将 math 目录里的文件编译成静态库再由 main 函数调用。


根目录中的 CMakeLists.txt :


# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (demo3)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 添加 math 子目录
add_subdirectory(math)
# 指定生成目标
add_executable(demo main.cpp)
# 添加链接库
target_link_libraries(demo MathFunctions)


该文件添加了下面的内容: add_subdirectory 指明本项目包含一个子目录 math,这样 math 目录下的 CMakeLists.txt 文件和源代码也会被处理 。使用命令 target_link_libraries 指明可执行文件 main 需要连接一个名为 MathFunctions 的链接库 。


子目录中的 CMakeLists.txt:


# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})


在该文件中使用命令 add_library 将 src 目录中的源文件编译为静态链接库。


【构建】——【编译】——【执行】


[root@hackett demo3]# mkdir build
[root@hackett demo3]# cd build/
[root@hackett build]# cmake ..
-- The C compiler identification is GNU 8.4.1
-- The CXX compiler identification is GNU 8.4.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/cmake/demo3/build
[root@hackett build]# make
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/myMath.cpp.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
[ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
[100%] Linking CXX executable demo
[100%] Built target demo
[root@hackett build]# ./demo 2 3 
2 + 3 is 5


参考文档:


CMake入门实战


CMake Tutorial


如果你觉得文章还不错,可以给个"三连",文章同步到个人微信公众号[加班猿]


我是hackett,我们下期见



目录
相关文章
|
网络协议 Docker 容器
Docker容器内不能联网的6种解决方案
Docker容器内不能联网的6种解决方案   注:下面的方法是在容器内能ping通公网IP的解决方案,如果连公网IP都ping不通,那主机可能也上不了网(尝试ping 8.
13357 2
鸿蒙开发:V2版本装饰器@Once
@Once装饰器只能与@Param搭配使用,仅此一个组合,无其他使用方式,还有就是,必须在V2版本,也就是@ComponentV2装饰的自定义组件中,否则会报异常。
244 7
鸿蒙开发:V2版本装饰器@Once
|
9月前
|
Web App开发 数据安全/隐私保护 Python
快手批量发布作品工具,自动上传视频发布软件,python实现自动脚本
这个脚本实现了快手批量上传视频的功能,包含登录、上传视频、添加描述和发布等完整流程
|
安全 数据安全/隐私保护 Android开发
深入探索iOS系统安全机制:从基础到高级
本文旨在全面解析iOS操作系统的安全特性,从基础的权限管理到高级的加密技术,揭示苹果如何构建一个既开放又安全的移动平台。我们将通过实例和分析,探讨iOS系统如何保护用户数据免受恶意软件、网络攻击的威胁,并对比Android系统在安全性方面的差异。
|
编解码 应用服务中间件 开发工具
如何在RTMP推送端和RTMP播放端支持Enhanced RTMP H.265(HEVC)
时隔多年,在Enhancing RTMP, FLV With Additional Video Codecs And HDR Support(2023年7月31号正式发布)官方规范出来之前,如果RTMP要支持H.265,大家约定俗成的做法是扩展flv协议,CDN厂商携手给出的解决方案是给flv的videotag CodecID增加一个新类型(12)来表示h265(hevc),和h264不同的地方是要解析HEVCDecoderConfigurationRecord,从HEVCDecoderConfigurationRecord中解析出vps, sps, pps. 有了vps, sps, pps,
700 6
|
存储
CMake中遍历元素的技巧:foreach命令详解
CMake中遍历元素的技巧:foreach命令详解
991 1
|
Rust JavaScript
Zed——Eslint配置支持Vue
Zed——Eslint配置支持Vue
432 0
|
应用服务中间件 nginx
使用Nginx搭建文件服务器
之前已经出了Nginx搭建和配置的文章,所以不再赘述,如有不会搭建看下面链接:
使用Nginx搭建文件服务器
|
运维 Kubernetes 监控
一文读懂蓝绿发布、A/B 测试和金丝雀发布的优缺点
目前,业界已经总结出了几种常见的服务发布策略来解决版本升级过程中带来的流量有损问题。本文首先会对这些普遍的发布策略进行简单的原理解析,最后结合阿里云的云原生网关对这些发布策略进行实践。
3508 95
一文读懂蓝绿发布、A/B 测试和金丝雀发布的优缺点
|
运维 监控 算法
【C/C++ 实用工具】网络监控工具一览
【C/C++ 实用工具】网络监控工具一览
350 0