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檔案內容

set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)

# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  )

TutorialConfig.h.in (.in也可改為其他名)

#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

之後cmake .完後會產生如下檔案TutorialConfig.h

#define Tutorial_VERSION_MAJOR 1
#define Tutorial_VERSION_MINOR 0

2.加入其他函式庫

target_link_libraries() 用來指定某個 target 要和哪些程式庫連結
add_library()  用來把程式製作成函式庫

以下可設定是否加入函式庫

cmakelist.txt

option (USE_MYMATH "Use tutorial provided math implementation" ON)  #設定on 或 off  https://cmake.org/cmake/help/v3.0/command/option.html

if (USE_MYMATH)                                                     # 根據on 或off 決定是否執行if內程式
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

然後再.h.in寫上

#cmakedefine USE_MYMATH
會在cmake編譯後轉為
#define USE_MYMATH

3.加上install()

make install 的功能為the install function / section in your Makefile

但因為我們現在是用cmake產生makefile所以我們要把我們要做的事寫在cmakelist.txt

然後執行後會出現如下 即可安裝可執行檔到電腦全域目錄

3-1 加上測試case

主要指令為ctest

可參考:https://cmake.org/cmake/help/v3.0/command/add_test.html

需在cmakelist.txt加上如下

include(CTest)  # 引入ctest

# does the application run
add_test (TutorialRuns Tutorial 25)     # add_test為主要測試用的函式  其內容為add_test(<name> <command> [<arg>...])

# does it sqrt of 25
add_test (TutorialComp25 Tutorial 25)
set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")

有關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

#define a macro to simplify adding tests, then use it
macro (do_test arg result)
  add_test (TutorialComp${arg} Tutorial ${arg})
  set_tests_properties (TutorialComp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)

# do a bunch of result based tests
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")

4.檢查平台作業系統是否有特定函式

include (CheckFunctionExists)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

之後再.h標頭檔加入以下 (cmakedefine找到cmakelist.txt內的變數並做替換)

#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP

5.於跑cmakelist.txt時也跑一些command

add_custom_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最後面加入以下

# build a CPack driven installer package
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE  
     "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
include (CPack)

然後一樣執行cmake .

然後

To build a binary distribution you would run:

cpack --config CPackConfig.cmake

To create a source distribution you would type

cpack --config CPackSourceConfig.cmake

#其他說明

 include_directories() 指令添加引入檔的查找目錄
 add_subdirectory() 指令的功能在於告訴 CMake 到子目錄下執行子目錄的 CMakeLists。

Last updated