Cmake
#Cmake 一個跨平台編譯工具
官方教學:https://cmake.org/cmake-tutorial/
官方文件:https://cmake.org/cmake/help/v3.0/manual/cmake-language.7.html#comments
中文教學:https://zh.wikibooks.org/wiki/CMake_入門
1.(CMake的指令沒有大小寫之分)
2.(CMake提供內建核心函數https://cmake.org/cmake/help/v3.0/manual/cmake-modules.7.html)Ex:
cmake_minimum_required(VERSION 2.6) # 檢查cmake版本 當發現版本不對時還是可以執行 但會發出警告來提醒你要確認版本
project(MyProject) # 用來指名 project 名稱, 作為之後參考的變數
include_directories(include) # 指定 include header path
set(IO_SOURCES src/input.cpp src/print.cpp) # 設定變數, 將第二個所給予的內容指定到第一個變數名稱當中
set(HELLO_SOURCES src/hello.cpp)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) # PROJECT....是CMAKE寫好的變數https://cmake.org/cmake/help/v3.0/manual/cmake-variables.7.html
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
add_library(io ${IO_SOURCES})
add_library(hello ${HELLO_SOURCES})
add_executable(main main.cpp) # 產生可執行檔
target_link_libraries(main io hello)1.使用cmakelist.txt 定義的變數替換.h檔案內容
TutorialConfig.h.in (.in也可改為其他名)
之後cmake .完後會產生如下檔案TutorialConfig.h
2.加入其他函式庫
以下可設定是否加入函式庫
cmakelist.txt
然後再.h.in寫上
3.加上install()
但因為我們現在是用cmake產生makefile所以我們要把我們要做的事寫在cmakelist.txt
在兩個cmakelist.txt分別加上以下(官方教學step3)
然後執行後會出現如下 即可安裝可執行檔到電腦全域目錄

3-1 加上測試case
主要指令為ctest
可參考:https://cmake.org/cmake/help/v3.0/command/add_test.html
需在cmakelist.txt加上如下
有關PASS_REGULAR_EXPRESSION 用來 verify that the output of the test contains certain strings
Macro(批量測試)
或是可以進行批量測試 使用macro函式
https://cmake.org/cmake/help/v3.0/command/macro.html
4.檢查平台作業系統是否有特定函式
之後再.h標頭檔加入以下 (cmakedefine找到cmakelist.txt內的變數並做替換)
5.於跑cmakelist.txt時也跑一些command
https://cmake.org/cmake/help/v3.0/command/add_custom_command.html
If more than one command is specified they will be executed in order. The optional ARGS argument is for backward compatibility and will be ignored.
6.建立一個可發佈的專案
building installation packages that support binary installations and package management features as found in cygwin, debian, RPMs etc
在cmakeList.txt最後面加入以下
然後一樣執行cmake .
然後
#其他說明
Last updated
Was this helpful?