如今进一步将MathFunctions.c和MathFunctions.h文件移到math目录下:函数
./Demo3ui
|spa
+--- main.ccode
|blog
+--- math/get
|io
+--- MathFunctions.c编译
|table
+--- MathFunctions.hclass
这种状况下,须要在根目录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 目录中的源文件编译为静态连接库。