前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >CMake入门实战——多个源文件

CMake入门实战——多个源文件

作者头像
AI异构
发布2020-07-29 14:57:59
1.8K0
发布2020-07-29 14:57:59
举报
文章被收录于专栏:AI异构AI异构

多个源文件

同一目录,多个源文件

上面的例子只有单个源文件。现在假如把 power 函数单独写进一个名为 MathFunctions.c 的源文件里,使得这个工程变成如下的形式:

代码语言:javascript
复制
./Demo2
    |
    +--- main.cc
    |
    +--- MathFunctions.cc
    |
    +--- MathFunctions.h

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

代码语言:javascript
复制
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo2)
# 指定生成目标
add_executable(Demo main.cc MathFunctions.cc)

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

代码语言:javascript
复制
aux_source_directory(<dir> <variable>)

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

代码语言:javascript
复制
# 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 的可执行文件。

代码语言:javascript
复制
xuke@ubuntu:~/work/cmake-demo/Demo2$ ls
CMakeLists.txt  main.cc  MathFunctions.cc  MathFunctions.h
xuke@ubuntu:~/work/cmake-demo/Demo2$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- 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/xuke/work/cmake-demo/Demo2
xuke@ubuntu:~/work/cmake-demo/Demo2$ ls
CMakeCache.txt  cmake_install.cmake  main.cc   MathFunctions.cc
CMakeFiles      CMakeLists.txt       Makefile  MathFunctions.h
xuke@ubuntu:~/work/cmake-demo/Demo2$ make
Scanning dependencies of target Demo
[ 33%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[ 66%] Building CXX object CMakeFiles/Demo.dir/MathFunctions.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
xuke@ubuntu:~/work/cmake-demo/Demo2$ ./Demo 3 2
3 ^ 2 is 9
多个目录,多个源文件

现在进一步将 MathFunctions.hMathFunctions.cc 文件移动到 math 目录下。

代码语言:javascript
复制
./Demo3
    |
    +--- main.cc
    |
    +--- math/
          |
          +--- MathFunctions.cc
          |
          +--- MathFunctions.h

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

根目录中的 CMakeLists.txt :
代码语言:javascript
复制
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo3)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 添加 math 子目录
add_subdirectory(math)
# 指定生成目标
add_executable(Demo main.cc)
# 添加链接库
target_link_libraries(Demo MathFunctions)

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

子目录中的 CMakeLists.txt:
代码语言:javascript
复制
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_LIB_SRCS 变量
aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library (MathFunctions ${DIR_LIB_SRCS})

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

结果
代码语言:javascript
复制
xuke@ubuntu:~/work/cmake-demo/Demo3$ ls
CMakeLists.txt  main.cc  math
xuke@ubuntu:~/work/cmake-demo/Demo3$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- 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/xuke/work/cmake-demo/Demo3
xuke@ubuntu:~/work/cmake-demo/Demo3$ ls
CMakeCache.txt  cmake_install.cmake  main.cc   math
CMakeFiles      CMakeLists.txt       Makefile
xuke@ubuntu:~/work/cmake-demo/Demo3$ cd math/
xuke@ubuntu:~/work/cmake-demo/Demo3/math$ ls
CMakeFiles           CMakeLists.txt  MathFunctions.cc
cmake_install.cmake  Makefile        MathFunctions.h
xuke@ubuntu:~/work/cmake-demo/Demo3/math$ cd ..
xuke@ubuntu:~/work/cmake-demo/Demo3$ ls
CMakeCache.txt  cmake_install.cmake  main.cc   math
CMakeFiles      CMakeLists.txt       Makefile
xuke@ubuntu:~/work/cmake-demo/Demo3$ make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
xuke@ubuntu:~/work/cmake-demo/Demo3$ ./Demo 3 2
3 ^ 2 is 9

参考

[CMake 入门实战] http://www.hahack.com/codes/cmake/ [代码参考] https://github.com/wzpan/cmake-demo

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2018-05-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI异构 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 多个源文件
    • 同一目录,多个源文件
      • 多个目录,多个源文件
        • 根目录中的 CMakeLists.txt :
        • 子目录中的 CMakeLists.txt:
        • 结果
    • 参考
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档