多個源文件,多個目錄
現在進一步將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 目錄中的源文件編譯為靜態鏈接庫。
