CMake入門-04-自定義編譯選項(xiàng)

工作環(huán)境

  • 系統(tǒng):macOS Mojave 10.14.6
  • CMake: Version 3.15.0-rc4

Hello,World! - 自定義編譯選項(xiàng)

CMake 允許為項(xiàng)目增加編譯選項(xiàng),從而可以根據(jù)用戶的環(huán)境和需求選擇最合適的編譯方案。

例如,可以將 MathFunctions 庫(kù)設(shè)為一個(gè)可選的庫(kù),如果該選項(xiàng)為 ON ,就使用該庫(kù)定義的數(shù)學(xué)函數(shù)來進(jìn)行運(yùn)算。否則就調(diào)用標(biāo)準(zhǔn)庫(kù)中的數(shù)學(xué)函數(shù)庫(kù)。

(0) 初始化項(xiàng)目

$ mkdir hello
$ cd hello
$ mkdir math build
$ touch CMakeLists.txt main.cpp math/MathFunctions.h math/MathFunctions.cpp math/CMakeLists.txt
$ tree
.
├── build
├── CMakeLists.txt
├── main.cpp
└── math
    ├── CMakeLists.txt
    ├── MathFunctions.cpp
    └── MathFunctions.h
  • math/MathFunctions.h
int power(int base, int exponent);
  • math/MathFunctions.cpp
#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"

int power(int base, int exponent) {
    int result = base;
    int i;

    if (exponent == 0) {
        return 1;
    }

    for(i = 1; i < exponent; ++i){
        result = result * base;
    }
    return result;
}
  • main.cpp
#include <iostream>
#include "MathFunctions.h"

using namespace std;

int main(int argc, char const *argv[]) {
  printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
  return 0;
}
  • CMakeLists.txt
# CMake 最低版本號(hào)要求
cmake_minimum_required(VERSION 3.15.0)

# 項(xiàng)目名
project(hello)

# 查找當(dāng)前目錄下的所有源文件,并將名稱保存到 SRC_LIST 變量
aux_source_directory(. SRC_LIST)
# 查找 math 目錄下的所有源文件,并將名稱保存到 MATH_SRC_LIST 變量
# aux_source_directory(${PROJECT_SOURCE_DIR}/math MATH_SRC_LIST)

# 添加 math 子目錄 (math 目錄里必須有 CMakeLists.txt),這樣 math 目錄下的 CMakeLists.txt 文件和源代碼也會(huì)被處理
add_subdirectory(math)

# 添加頭文件路徑
include_directories(${PROJECT_SOURCE_DIR}/math)

# 指定生成目標(biāo)
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

# 添加鏈接庫(kù)
target_link_libraries(hello MathFunctions)
  • math/CMakeLists.txt
# 查找當(dāng)前目錄下的所有源文件,并將名稱保存到 DIR_LIB_SRCS 變量
aux_source_directory(. DIR_LIB_SRCS)

# 生成鏈接庫(kù)
add_library (MathFunctions ${DIR_LIB_SRCS})

(1) 修改根目錄 CMakeLists.txt

cmake_minimum_required(VERSION 3.15.0)

# 項(xiàng)目名
project(hello)

# 查找當(dāng)前目錄下的所有源文件,并將名稱保存到 SRC_LIST 變量
aux_source_directory(. SRC_LIST)


# 加入一個(gè)配置頭文件,用于處理 CMake 對(duì)源碼的設(shè)置
configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_SOURCE_DIR}/config.h"
)

# 是否使用自己的 MathFunctions 庫(kù)
# 這里設(shè)置的變量 USE_MYMATH、中間的提示文字、默認(rèn)值,在 ccmake 命令中會(huì)展示
option (USE_MYMATH
  "Use provided math implementation"
  ON
)

# 是否加入 MathFunctions 庫(kù)
if (USE_MYMATH)
  # 添加頭文件路徑
  include_directories ("${PROJECT_SOURCE_DIR}/math")
  # 添加 math 子目錄 (math 目錄里必須有 CMakeLists.txt)
  add_subdirectory (math)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)



# 指定生成目標(biāo)
add_executable(hello ${SRC_LIST} ${MATH_SRC_LIST})

# 添加鏈接庫(kù)
target_link_libraries(hello ${EXTRA_LIBS})
  • configure_file 命令用于加入一個(gè)配置頭文件 config.h,這個(gè)文件由 CMake 從 config.h.in 生成,通過這樣的機(jī)制,將可以通過預(yù)定義一些參數(shù)和變量來控制代碼生成。
  • option 命令添加了一個(gè) USE_MYMATH 選項(xiàng),并且默認(rèn)值為 ON。
  • 根據(jù) USE_MYMATH 變量的值來決定是否使用我們自己編寫的 MathFunctions 庫(kù)。

(2) 修改 main.cpp 文件

#include <iostream>
#include "config.h"

#ifdef USE_MYMATH
  // 如果定義了 USE_MYMATH,導(dǎo)入 "MathFunctions.h"
  #include "MathFunctions.h"
#else
  // 如果 USE_MYMATH 未定義,導(dǎo)入 <cmath>
  #include <cmath>
#endif

using namespace std;

int main(int argc, char const *argv[]) {

  #ifdef USE_MYMATH
    printf("Here define USE_MYMATH \n");
    printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
  #else
    printf("Here undefine USE_MYMATH \n");
    printf("%s power(2,3)=%f \n", "Hello,World!", pow(2, 3));
  #endif

  return 0;
}

(3) 新建 config.h.in 文件

#cmakedefine USE_MYMATH
  • 這樣 CMake 會(huì)自動(dòng)根據(jù) config.h.in 配置文件中的設(shè)置自動(dòng)生成 config.h 文件。

(4) 編譯 & 運(yùn)行

  • cmake 命令編譯
$ cd Desktop/hello/build

# cmake 指定 USE_MYMATH=ON
$ cmake -DUSE_MYMATH=ON ..
-- The C compiler identification is AppleClang 10.0.1.10010046
-- The CXX compiler identification is AppleClang 10.0.1.10010046
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/staff/Desktop/hello/build

$  make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cpp.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target hello
[ 75%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

# 這里輸出的 ”Here define USE_MYMATH“
$ ./hello
Here define USE_MYMATH
Hello,World! power(2,3)=8

# cmake 指定 USE_MYMATH=OFF
$ cmake -DUSE_MYMATH=OFF ..
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/staff/Desktop/hello/build

$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello

# 這里輸出的 ”Here undefine USE_MYMATH“
$ ./hello
Here undefine USE_MYMATH
Hello,World! power(2,3)=8.000000
  • ccmake 命令編譯
$ cd Desktop/hello/build
$ ccmake ..

可以看到 USE_MYMATH 選項(xiàng)

ccmake-command
  • 鍵盤的方向鍵可以在不同的選項(xiàng)間切換
  • 按下 enter 鍵可以修改該選項(xiàng)
  • 修改完成后可以按下 c 選項(xiàng)完成配置,之后再按 g 鍵確認(rèn)生成 Makefile
  • ccmake 的其他操作可以參考窗口下方給出的指令提示

相關(guān)參考:
CMake 官方網(wǎng)站
CMake 入門實(shí)戰(zhàn)


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 參考 CMake入門實(shí)戰(zhàn)cmake緩存清理 什么是CMake CMake允許開發(fā)者編寫一種平臺(tái)無關(guān)的CMakeLi...
    GeminiGirl0604閱讀 3,566評(píng)論 0 3
  • 什么是 CMake CMake是個(gè)一個(gè)開源的跨平臺(tái)自動(dòng)化建構(gòu)系統(tǒng),用來管理軟件建置的程序,并不相依于某特定編譯器。...
    蘇州丸子閱讀 22,868評(píng)論 6 74
  • Android Studio 從 2.2 版本起開始支持 CMake ,可以通過 CMake 和 NDK 將 C/...
    glumes閱讀 1,546評(píng)論 0 8
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom閱讀 3,214評(píng)論 0 3
  • 在Android Studio 2.2開始,正式支持cmake編譯,在與android studio結(jié)合之前,cm...
    蛋西閱讀 6,432評(píng)論 0 3

友情鏈接更多精彩內(nèi)容