cmake從入門到精通(一)

BAT架構(gòu)師資料下載https://github.com/0voice/from_coder_to_expert
[toc]

概述

本項(xiàng)目的目的是逐步掌握cmake的使用,從最基本的單文件開始,到復(fù)雜工程的搭建能力。

實(shí)踐

案例1-單文件構(gòu)建

參考:https://cmake.org/cmake-tutorial/

對(duì)應(yīng)代碼:01-Tutorial

The most basic project is an executable built from source code files. For simple projects a two line CMakeLists.txt file is all that is required. This will be the starting point for our tutorial. The CMakeLists.txt file looks like:

cmake_minimum_required (VERSION 2.8)
project (01-Tutorial)
add_executable(tutorial 01-Tutorial.cpp)

Note that this example uses lower case commands in the CMakeLists.txt file. Upper, lower, and mixed case commands are supported by CMake. The source code for tutorial.cxx will compute the square root of a number and the first version of it is very simple, as follows:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

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

解析

cmake_minimum_required

Set the minimum required version of cmake for a project.

cmake_minimum_required(VERSION major[.minor[.patch[.tweak]]]
                       [FATAL_ERROR])

比如

cmake_minimum_required (VERSION 2.8)

project

參考地址:https://cmake.org/cmake/help/git-stage/command/project.html
Set the name of the project.

project(<PROJECT-NAME> [LANGUAGES] [<language-name>...])
project(<PROJECT-NAME>
        [VERSION <major>[.<minor>[.<patch>[.<tweak>]]]]
        [LANGUAGES <language-name>...])

指定項(xiàng)目的名稱寻拂。項(xiàng)目最終編譯生成的可執(zhí)行文件并不一定是這個(gè)項(xiàng)目名稱晨川,而是由另一條命令(add_executable)確定的刷喜,稍候我們?cè)俳榻B荣回。

add_executable

Add an executable to the project using the specified source files.

add_executable(<name> [WIN32] [MACOSX_BUNDLE]
               [EXCLUDE_FROM_ALL]
               [source1] [source2 ...])

定義了這個(gè)工程會(huì)生成一個(gè)文件名為 name的可執(zhí)行文件

案例2-單文件+版本號(hào)構(gòu)建

參考:https://cmake.org/cmake-tutorial/

對(duì)應(yīng)代碼:02-Tutorial
The first feature we will add is to provide our executable and project with a version number. While you can do this exclusively in the source code, doing it in the CMakeLists.txt file provides more flexibility. To add a version number we modify the CMakeLists.txt file as follows:

cmake_minimum_required (VERSION 2.8)
project (02-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 files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
 
# add the executable
add_executable(tutorial 02-Tutorial.cpp)

Since the configured file will be written into the binary tree we must add that directory to the list of paths to search for include files. We then create a TutorialConfig.h.in file in the source tree with the following contents:

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

When CMake configures this header file the values for @Tutorial_VERSION_MAJOR@ and @Tutorial_VERSION_MINOR@ will be replaced by the values from the CMakeLists.txt file. Next we modify tutorial.cxx to include the configured header file and to make use of the version numbers. The resulting source code is listed below.

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"

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

    fprintf(stdout,"%s Version %d.%d\n",
                argv[0],
                Tutorial_VERSION_MAJOR,
                Tutorial_VERSION_MINOR);

    double inputValue;
    if(argc == 2)
    {
        inputValue = atof(argv[1]);
    }
    else
    {
        inputValue = 4;
    }
    double outputValue = sqrt(inputValue);
    fprintf(stdout,"The square root of %g is %g\n",
            inputValue, outputValue);
    return 0;
}

解析

set

Set a normal, cache, or environment variable to a given value.
設(shè)置變量
參考:https://cmake.org/cmake/help/git-stage/command/set.html?highlight=set

  • Set Normal Variable
    set(<variable> <value>... [PARENT_SCOPE])
  • Set Cache Entry
    set(<variable> <value>... CACHE <type> <docstring> [FORCE])
  • Set Environment Variable
    set(ENV{<variable>} <value>...)

PROJECT_SOURCE_DIR

Top level source directory for the current project.
即是工程的頂級(jí)目錄

PROJECT_BINARY_DIR

Full path to build directory for project.
即是編譯目錄,比如如果你創(chuàng)建了build目錄(cd build和cmake ..)雹锣,則路徑為:

PROJECT_SOURCE_DIR/build

如果在工程頂級(jí)目錄直接進(jìn)行編譯(cmake .)則和PROJECT_SOURCE_DIR一致

案例3-單文件+庫文件調(diào)用

頂層目錄內(nèi)的文件內(nèi)容

先編譯庫文件

庫文件放在MathFunctions目錄网沾。
Now we will add a library to our project. This library will contain our own implementation for computing the square root of a number. The executable can then use this library instead of the standard square root function provided by the compiler. For this tutorial we will put the library into a subdirectory called MathFunctions. It will have the following one line CMakeLists.txt file:

  1. 添加CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
add_library(MathFunctions mysqrt.cpp)
  1. 添加頭文件MathFunctions.h
#ifndef __MATH_FUNCTION_H__
#define __MATH_FUNCTION_H__
double mysqrt(double input);
#endif
  1. 添加實(shí)現(xiàn)文件
#include <math.h>
#include <stdio.h>
double mysqrt(double input)
{
    printf("call mysqrt\n");
    return sqrt(input);
}

此時(shí)目錄文件為:
  1. 創(chuàng)建build目錄并進(jìn)行編譯
mkdir build
cd build
cmake ..
make

此時(shí)build目錄下生成libMathFunctions.a文件,將其拷貝到上一級(jí)目錄(即是MathFunctions)

cp libMathFunctions.a ../
cd ..
ls
#可以看到當(dāng)前l(fā)ibMathFunctions目錄的內(nèi)容
CMakeLists.txt  MathFunctions.cpp  MathFunctions.h  build  libMathFunctions.a

編譯main函數(shù)所在文件

  1. 頂層目錄CMakeLists.txt文件
cmake_minimum_required (VERSION 2.8)
project (03-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 files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")

include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) 

 
# add the executable
add_executable(tutorial 03-Tutorial.cpp)
target_link_libraries (tutorial MathFunctions)

  1. main函數(shù)所在文件
    03-Tutorial.cpp
// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#include "MathFunctions.h"

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

    fprintf(stdout,"%s Version %d.%d\n",
                argv[0],
                Tutorial_VERSION_MAJOR,
                Tutorial_VERSION_MINOR);

    double inputValue;
    if(argc == 2)
    {
        inputValue = atof(argv[1]);
    }
    else
    {
        inputValue = 4;
    }
    double outputValue = mysqrt(inputValue);
    fprintf(stdout,"The square root of %g is %g\n",
            inputValue, outputValue);
    return 0;
}
  1. 編譯和執(zhí)行
mkdir build
cd build
cmake ..
make

執(zhí)行文件./tutorial

lqf@ubuntu:/mnt/hgfs/linux/multimedia/src/project/cmake_learn/03-Tutorial/build$ ./tutorial 
./tutorial Version 1.0
call mysqrt
The square root of 4 is 2

參考文檔

[1] cmake-tutorial

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蕊爵,一起剝皮案震驚了整個(gè)濱河市辉哥,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌攒射,老刑警劉巖醋旦,帶你破解...
    沈念sama閱讀 219,110評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異会放,居然都是意外死亡饲齐,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門咧最,熙熙樓的掌柜王于貴愁眉苦臉地迎上來捂人,“玉大人御雕,你說我怎么就攤上這事±拇睿” “怎么了酸纲?”我有些...
    開封第一講書人閱讀 165,474評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)瑟匆。 經(jīng)常有香客問我闽坡,道長(zhǎng),這世上最難降的妖魔是什么愁溜? 我笑而不...
    開封第一講書人閱讀 58,881評(píng)論 1 295
  • 正文 為了忘掉前任疾嗅,我火速辦了婚禮,結(jié)果婚禮上祝谚,老公的妹妹穿的比我還像新娘宪迟。我一直安慰自己酣衷,他們只是感情好交惯,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,902評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著穿仪,像睡著了一般席爽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上啊片,一...
    開封第一講書人閱讀 51,698評(píng)論 1 305
  • 那天只锻,我揣著相機(jī)與錄音,去河邊找鬼紫谷。 笑死齐饮,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的笤昨。 我是一名探鬼主播祖驱,決...
    沈念sama閱讀 40,418評(píng)論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼瞒窒!你這毒婦竟也來了捺僻?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤崇裁,失蹤者是張志新(化名)和其女友劉穎匕坯,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體拔稳,經(jīng)...
    沈念sama閱讀 45,796評(píng)論 1 316
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡葛峻,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,968評(píng)論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了巴比。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片术奖。...
    茶點(diǎn)故事閱讀 40,110評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡逼侦,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出腰耙,到底是詐尸還是另有隱情榛丢,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評(píng)論 5 346
  • 正文 年R本政府宣布挺庞,位于F島的核電站晰赞,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏选侨。R本人自食惡果不足惜掖鱼,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,455評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望援制。 院中可真熱鬧戏挡,春花似錦、人聲如沸晨仑。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽洪己。三九已至妥凳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間答捕,已是汗流浹背逝钥。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留拱镐,地道東北人艘款。 一個(gè)月前我還...
    沈念sama閱讀 48,348評(píng)論 3 373
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像沃琅,于是被迫代替她去往敵國(guó)和親哗咆。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,047評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容

  • CMake學(xué)習(xí) 本篇分享一下有關(guān)CMake的一些學(xué)習(xí)心得以及相關(guān)使用阵难。 本文目錄如下: [1岳枷、CMake介紹] [...
    AlphaGL閱讀 12,247評(píng)論 11 79
  • pyspark.sql模塊 模塊上下文 Spark SQL和DataFrames的重要類: pyspark.sql...
    mpro閱讀 9,457評(píng)論 0 13
  • 注:首發(fā)地址 1. 前言 當(dāng)在做 Android NDK 開發(fā)時(shí),如果不熟悉用 CMake 來構(gòu)建呜叫,讀不懂 CMa...
    cfanr閱讀 24,396評(píng)論 1 53
  • 王陸良閱讀 781評(píng)論 0 6
  • 輕快的音樂 沉穩(wěn)的聲線 伴隨著奔跑的你 一路成長(zhǎng) 夜空中疏散的星辰 睡夢(mèng)中遠(yuǎn)方的呼喚 醒來不過頹唐一人 擊碎了幻想...
    雨落今閱讀 162評(píng)論 0 0