CMake入門-03-還是HelloWorld

工作環境

  • 系統:macOS Mojave 10.14.6
  • CMake: Version 3.15.0-rc4

Hello,World! 擴展-math 目錄裏的文件編譯成靜態庫再由 main 函數調用

(0) 初始化項目

$ 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

(1) 準備測試代碼 main.cpp、math/MathFunctions.h、math/MathFunctions.cpp

  • 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[]) {
  /* code */
  // cout << "Hello,World!" << power(2, 3) << endl;
  printf("%s power(2,3)=%d \n", "Hello,World!", power(2, 3));
  return 0;
}

(2) 準備 CMakeLists.txt math/CMakeLists.txt

  • CMakeLists.txt
# CMake 最低版本號要求
cmake_minimum_required(VERSION 3.15.0)

# 項目名
project(hello)

# 查找當前目錄下的所有源文件,並將名稱保存到 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 文件和源代碼也會被處理
add_subdirectory(math)

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

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

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

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

(3) 編譯運行

$ cd hello/build
$ cmake ..
-- 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

$ ./hello
Hello,World! power(2,3)=8

相關參考:
CMake 官方網站
CMake 入門實戰

聯繫作者:
聯繫作者


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章