CMake Tutorial (1)——A Basic Starting Point

最基礎的程序項目是通過對源碼文件的構建產生可執行文件。對於簡單的工程,你只需要在CMakeLists.txt文件中寫入幾行文本就足夠了。這將是學習CMake的起點,CMakeLists.txt的文件內容與下面類似:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable (Tutorial tutorial.cxx)

在上面的示例文件CMakeLists.txt中,使用的都是小寫的英文字母。當然你也可以採用大寫,或則大小寫混合使用,CMake工具都是支持的。tutorial.cxx源文件的第一個版本如下,它會計算所給數值的平方根:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
using namespace std;

int main(int argc, char *argv[])
{
    if (argc < 2) {
        fprintf(stdout, "usage: %s number\n", argv[0]);
        return 1;
    }
    double inputValue = atof(argv[1]);
    double outputValue = sqrt(inputValue);
    fprintf(stdout, "The square root of %g is %g\n",
            inputValue, outputValue
    );
    return 0;
}

添加版本號並配置頭文件
現在我們爲我們的工程和程序添加一個版本號(Version number)。當然你也可以選擇在源代碼中這麼做,但是將其定義在CMakeLists.txt中會更加靈活,特別是當項目越來越大時。現在爲了添加版本號,我們將CMakeLists.txt文件修改成下面這樣:

# Minimum version required
cmake_minimum_required(VERSION 3.0.0)

PROJECT(Tutorial)

# The version number
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"
)

# add the binary tree to the search path for include file
# so that we will find TestConfig.h
include_directories("${PROJECT_BINARY_DIR}")

# add the executable
ADD_EXECUTABLE(tutorial tutorial.cpp)

先解釋一下上面的兩個東西,“PROJECT_SOURCE_DIR和PROJECT_BINARY_DIR”,你可以把它們看做是兩個常量量,它們是CMake系統內部已經定義好的。前者指代的是你的源碼目錄,後者指的是你的構建文件所在的目錄,網上有部分人認爲它們沒有區別,其實是有的,主要看你的習慣。一般在使用CMake時,我們會新建一個名爲build的文件夾,看起來像這樣。

drwxrwxr-x 3 black black 4.0K 318 13:23 build
-rw-rw-r-- 1 black black  539 318 13:10 CMakeLists.txt
-rw-rw-r-- 1 black black  149 318 11:58 TestConfig.h.in
-rw-rw-r-- 1 black black  602 318 12:03 test.cpp

build爲PROJECT_BINARY_DIR,而test.cpp文件所在的目錄爲PROJECT_SOURCE_DIR。但是你如果直接在test.cpp所在目錄使用CMake命令進行初始化,那麼上面的兩個常量量指的就是同一個目錄了。

現在再來看上面的CMakeLists.txt文件,我們先用set定義了兩個常量,然後再添加configure_file(配置文件的命令),接着你需要將TutorialConfig.h文件的位置告訴CMake,也就是通過include_directories將所需目錄包含。然後我們需要創建一個TutorialConfig.h文件(保存在source tree中,也就是和源碼在同一目錄),文件內容如下:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

完成後,當CMake工具配置.in文件中@Tutorial_VERSION_MAJOR@和@Tutorial_VERSION_MINOR@的值時,會用在CMakeLists.txt文件中的值(Tutorial_VERSION_MAJOR即1和Tutorial_VERSION_MINOR即0)將它們兩個替代,並在PROJECT_BINARY_DIR中生成一個TutorialConfig.h文件。接下來我們修改tutorial.cxx文件,讓其包含配置頭文件並使用version_number。修改後如下:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include "TutorialConfig.h"
using namespace std;

int main(int argc, char *argv[])
{
    if (argc < 2) {
        fprintf(stdout, "%s Version %d.%d\n",
                argv[0],
                Tutorial_VERSION_MAJOR,
                Tutorial_VERSION_MINOR
        );
        fprintf(stdout, "usage: %s number\n", argv[0]);
        return 1;
    }
    double inputValue = atof(argv[1]);
    double outputValue = sqrt(inputValue);
    fprintf(stdout, "The square root of %g is %g\n",
            inputValue, outputValue
    );
    return 0;
}

文件包含了TutorialConfig.h文件,並將version number作爲使用消息輸出。輸出如下:

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