CMake系列之四:多个源文件-多个目录

多个源文件,多个目录

现在进一步将MathFunctions.c和MathFunctions.h文件移到math目录下:

./Demo3

  |

  +--- main.c

  |

  +--- math/

     |

     +--- MathFunctions.c

     |

     +--- MathFunctions.h

CMakeLists.txt编写

这种情况下,需要在根目录Demo3和子目录math下各写一个CMakeLists.txt文件。为了方便,可以将math目录的文件编译成静态库,再由main函数调用

根目录下的CMakeLists.txt文件如下:

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (Demo3)
# 查找当前目录下的所有源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 添加头文件路径
include_directories("${PROJECT_SOURCE_DIR}/math")
# 添加 math 子目录
add_subdirectory(math)
# 指定生成目标
add_executable(Demo main.c)
# 添加链接库
target_link_libraries(Demo MathFunctions)

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

子目录的CMakeList.txt如下:

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

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

posted @ 2018-04-23 09:49  且听风吟-wuchao  阅读(34908)  评论(0编辑  收藏  举报