淺學CMake

      CMake是一個跨平臺的安裝(編譯)工具,能夠用簡單的語句來描述全部平臺的安裝(編譯過程)。CMake不構建出最終的可執行文件,而是產生一個標準的建構檔(即根據目標用戶的平臺進一步生成所需的本地化 Makefile 和工程文件),而後再依通常的建構方式使用,從而實現軟件的跨平臺。linux

<1>Linux下CMake的安裝c++

先到官網下載CMake源碼包:https://cmake.org/download/bootstrap

打開終端依次執行如下命令:windows

tar -xzvf cmake-2.6.4.tar.gz
cd cmake-2.6.4
 ./bootstrap
make
make install

cmake 會默認安裝在 /usr/local/bin 下面。(windows下安裝能夠直接下載安裝包進行安裝)ide

<2>Linux下使用CMake生成makefile & 編譯的大體流程以下:函數

1.編寫CMake的配置文件CMakeLists.txt(組態檔的命名和類型必須爲這個);工具

2.而後在終端輸入CMake <PATH>(PATH是 CMakeLists.txt 所在的目錄)或者CMake .(若是是在當前目錄下,注意CMake和.之間有空格)來生成Makefile;學習

3.最後在終端輸入make命令進行編譯(windows平臺下會生成VS的工程文件,可使用VS工具進行編譯)輸出可執行文件。測試

以上就是CMake簡單使用步驟,下面本文將會經過一些簡單的例子來說解CMake的一個基本使用。ui

<3>實例解析

主題一:單個源文件

假設如今咱們有個源文件demo1.c,該程序實現對某個數求絕對值

#include <stdio.h>
#include <stdlib.h>

int abs(int num){
    return num>=0?num:-num;
}

void main(int argc, char* argv[]){
    int num = 0;
    if(argc < 2){
        printf("usage: %s number\n", argv[0]);
  }
    else{
        num = atoi(argv[1]);
        printf("abs(%d) = %d\n", num, abs(num));
  }
}

接着在demo1.c源文件的目錄下,編寫CMakeLists.txt

#CMake中#後面的語句解釋爲註釋行
#指定cmake最低使用版本號,能夠不設置,可是會有一個warnning cmake_minimum_required(VERSION
2.8) #設置項目工程的名稱 project(Demo1) #將demo1.c源文件編譯成一個名爲Demo1的可執行文件 add_executable(Demo1 demo1.c)

而後在當前的目錄下,輸入cmake .命令生成makefile,最後再輸入make命令(windows下使用VS打開.sln工程文件進行編譯)來編譯獲得最後的可執行文件Demo1

[Tino@Tino-linux demo_test]$ cmake .
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /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: /usr/bin/c++
-- Check for working CXX compiler: /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: /home/Tino/Documents/demo_test
[Tino@Tino-linux demo_test]$ make
Scanning dependencies of target Demo1
[ 50%] Building C object CMakeFiles/Demo1.dir/demo1.c.o
[100%] Linking C executable Demo1
[100%] Built target Demo1
[Tino@Tino-linux demo_test]$ ./Demo1 -5
abs(-5) = 5
[Tino@Tino-linux demo_test]$ 

主題二:多個源文件

(1)同一目錄

假設如今咱們把上面例子中的求絕對值abs單獨編寫一個另一個源文件MyFunction.c中,以下形式(全部源文件都在同一目錄demo_test下)

/demo_test
    demo2.c
    MyFunction.h
    MyFunction.c

此時的CMakeLists.txt應修改以下

cmake_minimum_required(VERSION 2.8)
project(Demo2)
add_executable(Demo2 demo2.c MyFunction.c)

相對於上例的CMakeLists.txt,只是在add_executable中多添加了一個源文件。這樣寫過當然沒有問題,可是考慮到多文件的狀況,咱們這樣手動的添加多個源文件是否是太麻煩了呢?是的,這個時候咱們能夠這樣來添加文件

cmake_minimum_required(VERSION 2.8)
project(Demo2)
#該指令會查找所指定目錄下(. 表明當前目錄)的全部源文件 #並把文件名賦值給變量DIR_SOURCE aux_source_directory(. DIR_SOURCE) add_executable(Demo2 ${DIR_SOURCE})

這樣就能夠順利編譯出可執行文件了。

(2)不一樣目錄

如今在demo_test目錄下新建一個子目錄math,將咱們的MyFunction.h和MyFunction.c文件移動math文件裏面。這種狀況下,咱們就必需要爲每一個源文件目錄編寫一個CMakeLists.txt。文件結構以下

/demo_test
    demo03
    /math
        MyFunction.h
        MyFunction.c

 

首先在子目錄math裏面,編寫CMakeLists.txt

aux_source_directory(. DIR_LIB_SOURCE)
#將目錄中的源文件編譯爲靜態連接庫
add_library(MyFunction ${DIR_LIB_SOURCE})

在根目錄demo_test中的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Demo3)
aux_source_directory(. DIR_SOURCE)
#添加一個子目錄math,這樣子目錄下的CMakeLists.txt也會被處理 add_subdirectory(math) add_executable(Demo3 ${DIR_SOURCE}) #爲可執行文件添加咱們所須要的連接庫MyFunction target_link_libraries(Demo3 MyFunction)

接着咱們再輸入cmake命令:

[Tino@Tino-linux demo_test]$ cmake .
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /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: /usr/bin/c++
-- Check for working CXX compiler: /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: /home/Tino/Documents/demo_test

這樣根目錄和子目錄都會生成makefile,再make編譯成可執行文件就能夠了

[Tino@Tino-linux demo_test]$ make
Scanning dependencies of target MyFunction
[ 25%] Building C object math/CMakeFiles/MyFunction.dir/MyFunction.c.o
[ 50%] Linking C static library libMyFunction.a
[ 50%] Built target MyFunction
Scanning dependencies of target Demo3
[ 75%] Building C object CMakeFiles/Demo3.dir/demo3.c.o
[100%] Linking C executable Demo3
[100%] Built target Demo3
[Tino@Tino-linux demo_test]$ ./Demo3 9
abs(9) = 9
[Tino@Tino-linux demo_test]$ 

 主題三:自定義編譯選項

CMake容許用戶增長編譯選項,能夠根據用戶的環境和須要選擇最合適的編譯方案

好比咱們能夠把demo3中的MyFunction庫設計爲一個可選連接庫,CMakeLists.txt以下

cmake_minimum_required(VERSION 2.8)
project(Demo4)
#加入一個配置文件config.h,由CMake經過config.h.in生成 #這樣的機制能夠經過預約義一些參數和變量來控制代碼的生成 configure_file("${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h") #添加一個USE_MYMATH控制變量,默認值爲ON option(USE_MYMATH "USE MyFunction" ON) #根據是否認義USE_MYMATH進行條件編譯 if(USE_MYMATH) include_directories("${PROJECT_SOURCE_DIR}/math") add_subdirectory(math) set(EXTRA_LIBS ${EXTRA_LIB} MyFunction) endif(USE_MYMATH)
aux_source_directory(. DIR_SOURCE)
add_executable(Demo4 ${DIR_SOURCE})
target_link_libraries(Demo4 MyFunction ${EXTRA_LIB})

再修改源文件demo04.c

#include <stdio.h>
#include <stdlib.h>
#include "config.h" #ifdef USE_MYMATH #include "math/MyFunction.h" #else #include <math.h> #endif(USE_MYMATH) void main(int argc, char* argv[]){ int num = 0; if(argc < 2){ printf("usage: %s number\n", argv[0]); } else{ num = atoi(argv[1]); #ifdef USE_MYMATH printf("MyFunction: abs(%d) = %d\n", num, abs(num)); #else printf("SystemFunction: abs(%d) = %d\n", num, abs(num)); #endif(USE_MYMATH) } }

 編寫config.h.in文件

#cmakedefine USE_MYMATH

生成makefile,由於上面咱們定義了一個變量USE_MYMATH,其默認值爲ON,可是若是咱們想顯式的修改該變量的值,能夠輸入cmake -DUSE_MYMATH=OFF命令來修改變量的值爲OFF(cmake其餘的一些用法能夠鍵入cmake -help來查詢),一樣能夠在cmake生成makefile以後輸入命令make edit_cache或者ccmake .來激活一個配置窗口

具體的操做在這裏我就不細講了(窗口底下有指令提示),當USE_MYMATH爲ON時,config.h文件的內容爲 #define USE_MYMATH,即執行咱們自定的函數;當USE_MYMATH爲OFF時,config.h文件的內容爲/*#undef SE_MYMATH*/,執行系統庫函數

 主題四:安裝和測試

CMake也能夠指定安裝規則和添加測試,下面來看看具體實現

(1)定製安裝規則

如今math子目錄下的CMakeLists.txt

aux_source_directory(. DIR_LIB_SOURCE)
add_library(MyFunction ${DIR_LIB_SOURCE})
#指定MyFunction 安裝路徑爲當前目錄的bin子目錄下 #直接寫bin會默認安裝在/usr/local/bin中 install(TARGETS MyFunction DESTINATION "${PROJECT_SOURCE_DIR}/bin") #指定MyFunction.h 安裝路徑爲當前目錄的include子目錄下 #直接寫include會默認安裝在/usr/local/include中 install(FILES MyFunction.h DESTINATION "${PROJECT_SOURCE_DIR}/include")

修改根目錄demo_test下的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Demo5)
configure_file("${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h")
option(USE_MYMATH "USE MyFunction" ON)
if(USE_MYMATH)
    include_directories("${PROJECT_SOURCE_DIR}/math")
    add_subdirectory(math)
    set(EXTRA_LIBS ${EXTRA_LIB} MyFunction)
endif(USE_MYMATH)
aux_source_directory(. DIR_SOURCE)
add_executable(Demo5 ${DIR_SOURCE})
target_link_libraries(Demo5 MyFunction ${EXTRA_LIB})

install(TARGETS Demo5 DESTINATION "${PROJECT_SOURCE_DIR}/bin") install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION "${PROJECT_SOURCE_DIR}/include")

鍵入make install命令來安裝文件。這樣,經過上面的定製,生成MyFunction.h和config.h就會包含在demo_test目錄下的include文件中,而libMyFunction.o函數庫文件和Demo5可執行文件就會包含在demo_test目錄下的bin文件中

[Tino@Tino-linux demo_test]$ cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /home/Tino/Documents/demo_test
[Tino@Tino-linux demo_test]$ cmake .
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /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: /usr/bin/c++
-- Check for working CXX compiler: /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: /home/Tino/Documents/demo_test
[Tino@Tino-linux demo_test]$ make install
Scanning dependencies of target MyFunction
[ 25%] Building C object math/CMakeFiles/MyFunction.dir/MyFunction.c.o
[ 50%] Linking C static library libMyFunction.a
[ 50%] Built target MyFunction
Scanning dependencies of target Demo5
[ 75%] Building C object CMakeFiles/Demo5.dir/demo5.c.o
/home/Tino/Documents/demo_test/demo5.c:8:7: warning: extra tokens at end of #endif directive
/home/Tino/Documents/demo_test/demo5.c:21:15: warning: extra tokens at end of #endif directive
[100%] Linking C executable Demo5
[100%] Built target Demo5
Install the project...
-- Install configuration: ""
-- Installing: /home/Tino/Documents/demo_test/bin/Demo5 -- Installing: /home/Tino/Documents/demo_test/include/config.h -- Installing: /home/Tino/Documents/demo_test/bin/libMyFunction.a -- Installing: /home/Tino/Documents/demo_test/include/MyFunction.h

(2)爲工程添加測試

 修改根目錄下的CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(Demo6)
configure_file("${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h")
option(USE_MYMATH "USE MyFunction" ON)
if(USE_MYMATH)
    include_directories("${PROJECT_SOURCE_DIR}/math")
    add_subdirectory(math)
    set(EXTRA_LIBS ${EXTRA_LIB} MyFunction)
endif(USE_MYMATH)
aux_source_directory(. DIR_SOURCE)
add_executable(Demo6 ${DIR_SOURCE})
target_link_libraries(Demo6 MyFunction ${EXTRA_LIB})

install(TARGETS Demo6 DESTINATION "${PROJECT_SOURCE_DIR}/bin")
install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION "${PROJECT_SOURCE_DIR}/include")

#開始測試 enable_testing()
#添加一個測試(test_-9) add_test(test_
-9 Demo6 -9)
#設置測試屬性,PASS_REGULAR_EXPRESSION用來測試輸出是否包含雙引號裏面的字符串 set_tests_properties(test_
-9 PROPERTIES PASS_REGULAR_EXPRESSION "= 9")
#下同 add_test(test_8 Demo6
8) set_tests_properties(test_8 PROPERTIES PASS_REGULAR_EXPRESSION "= 8")

編譯後執行make test以下

[Tino@Tino-linux demo_test]$ make test
Running tests...
Test project /home/Tino/Documents/demo_test
    Start 1: test_-9
1/2 Test #1: test_-9 ..........................   Passed    0.00 sec
    Start 2: test_8
2/2 Test #2: test_8 ...........................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 2

Total Test time (real) =   0.01 sec
[Tino@Tino-linux demo_test]$ 

經過上面例子咱們已經知道能夠經過CMake來進行測試,可是不少時候,咱們測試的是要屢次反覆測量,才能儘量避免bug的出現,因此這個時候用add_test來一個個添加測試就顯得有力不從心了。所以,咱們須要定義一個宏來實現測試

cmake_minimum_required(VERSION 2.8)
project(Demo6)
configure_file("${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h")
option(USE_MYMATH "USE MyFunction" ON)
if(USE_MYMATH)
    include_directories("${PROJECT_SOURCE_DIR}/math")
    add_subdirectory(math)
    set(EXTRA_LIBS ${EXTRA_LIB} MyFunction)
endif(USE_MYMATH)
aux_source_directory(. DIR_SOURCE)
add_executable(Demo6 ${DIR_SOURCE})
target_link_libraries(Demo6 MyFunction ${EXTRA_LIB})

install(TARGETS Demo6 DESTINATION "${PROJECT_SOURCE_DIR}/bin")
install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION "${PROJECT_SOURCE_DIR}/include")

enable_testing()
add_test(test_-9 Demo6 -9)
set_tests_properties(test_-9 PROPERTIES PASS_REGULAR_EXPRESSION "= 9")
add_test(test_8 Demo6 8)
set_tests_properties(test_8 PROPERTIES PASS_REGULAR_EXPRESSION "= 8")

#定義一個宏 macro(do_test arg result) add_test(test_${arg} Demo6 ${arg}) set_tests_properties(test_${arg} PROPERTIES PASS_REGULAR_EXPRESSION ${result}) endmacro(do_test)
#使用宏進行測試 do_test(
7 "= 7") do_test(-6 "= 6")

 主題五:添加環境檢查

 有時候可能要對系統的環境作點檢查,好比要使用一個平臺的相關特性時。在如下例子中,咱們要檢查系統是否自帶abs函數,有則使用,沒有則使用自定義的abs函數。和上面咱們自定義編譯選項的作法相似

cmake_minimum_required(VERSION 2.8)
project(Demo7)

#添加CheckFunctionExists.cmake宏 include(${CMAKE_ROOT}
/Modules/CheckFunctionExists.cmake)
#調用check_function_exists命令測試連接器是否可以在連接階段找到abs函數 check_function_exists(abs HAVE_ABS) configure_file(
"${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h") if(NOT HAVE_ABS) include_directories("${PROJECT_SOURCE_DIR}/math") add_subdirectory(math) set
(EXTRA_LIBS ${EXTRA_LIB} MyFunction) endif(HAVE_ABS) aux_source_directory(. DIR_SOURCE) add_executable(Demo7 ${DIR_SOURCE}) target_link_libraries(Demo7 MyFunction ${EXTRA_LIB})

修改config.h.in文件

#cmakedefine HAVE_ABS

最後修改demo7.c

#include <stdio.h>
#include <stdlib.h>
#include "config.h" #ifdef HAVE_ABS #include <math.h>
#else #include "math/MyFunction.h"
#endif

void main(int argc, char* argv[]){
    int num = 0;
    if(argc < 2){
        printf("usage: %s number\n", argv[0]);
    }
    else{
        num = atoi(argv[1]);
 #ifdef HAVE_ABS printf("SystemFunction: abs(%d) = %d\n", num, abs(num)); #else      printf("MyFunction: abs(%d) = %d\n", num, abs(num)); #endif
    }
}

最後編譯,這裏我就不演示了,和上面例子相似

主題六:添加版本號

修改根目錄demo_test裏的CMakeLists.txt

 

cmake_minimum_required(VERSION 2.8)
project(Demo8)

#設定當前主、副版本號也就是變量的值
set(Demo8_VERSION_MAJOR 1) set(Demo8_VERSION_MINOR 0) include(${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake) check_function_exists(abs HAVE_ABS) configure_file("${PROJECT_SOURCE_DIR}/config.h.in" "${PROJECT_BINARY_DIR}/config.h") if(NOT HAVE_ABS) include_directories("${PROJECT_SOURCE_DIR}/math") add_subdirectory(math) set(EXTRA_LIBS ${EXTRA_LIB} MyFunction) endif(NOT HAVE_ABS) aux_source_directory(. DIR_SOURCE) add_executable(Demo8 ${DIR_SOURCE}) target_link_libraries(Demo8 MyFunction ${EXTRA_LIB}) install(TARGETS Demo8 DESTINATION "${PROJECT_SOURCE_DIR}/bin") install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION "${PROJECT_SOURCE_DIR}/include")

 

而後在修改config.h.in內容

#cmakedefine HAVE_ABS
#添加兩個預約義變量
#define Demo8_VERSION_MAJOR @Demo8_VERSION_MAJOR@ #define Demo8_VERSION_MINOR @Demo8_VERSION_MINOR@

在demo8.c裏面打印出咱們設置的版本信息

#include <stdio.h>
#include <stdlib.h>
#include "config.h"

#ifdef HAVE_ABS
    #include <math.h>
#else
    #include "math/MyFunction.h"
#endif

void main(int argc, char* argv[]){
    int num = 0;
    printf("%s Version %d.%d\n", argv[0], Demo8_VERSION_MAJOR, Demo8_VERSION_MINOR); if(argc < 2){
        printf("usage: %s number\n", argv[0]);
    }
    else{
        num = atoi(argv[1]);
        #ifdef HAVE_ABS
                printf("SystemFunction: abs(%d) = %d\n", num, abs(num));
        #else
        printf("MyFunction: abs(%d) = %d\n", num, abs(num));
        #endif
    }
}

執行看下結果

 

 

[Tino@Tino-linux demo_test]$ ./Demo8 5 ./Demo8 Version 1.0
MyFunction: abs(5) = 5

 

主題七:生成安裝包

利用CMake來配置生成各類平臺上的安裝包(二進制包和源碼包)

修改主目錄CMakeLists.txt文件

cmake_minimum_required(VERSION 2.8)
project(Demo9)

set(Demo9_VERSION_MAJOR 1)
set(Demo9_VERSION_MINOR 0)

include(${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists(abs HAVE_ABS)

configure_file("${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h")
if(NOT HAVE_ABS)
    include_directories("${PROJECT_SOURCE_DIR}/math")
    add_subdirectory(math)
    set(EXTRA_LIBS ${EXTRA_LIB} MyFunction)
endif(NOT HAVE_ABS)
aux_source_directory(. DIR_SOURCE)
add_executable(Demo9 ${DIR_SOURCE})
target_link_libraries(Demo9 MyFunction ${EXTRA_LIB})

#必須定製安裝規則,不然安裝包爲空,注意存放路徑 install(TARGETS Demo9 DESTINATION
bin) install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
#構建一個CPack安裝包
#導入InstallRequiredSystemLibraries模塊,以便以後導入CPack模塊 include(InstallRequiredSystemLibraries)
#設置CPack的基本信息
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/License.txt") set(CPACK_PACKAGE_VERSION_MAJOR "${Demo9_VERSION_MAJOR}") set(CPACK_PACKAGE_VERSION_MINOR "${Demo9_VERSION_MINOR}")
#導入CPack模塊
include(CPack)

修改子目錄math文件下的CMakeLists.txt

aux_source_directory(. DIR_LIB_SOURCE)
add_library(MyFunction ${DIR_LIB_SOURCE})

install(TARGETS MyFunction DESTINATION bin) install(FILES MyFunction.h DESTINATION include)

上面有3個要注意的地方,1.首先必需要定製安裝規則,即加入install指令;2.再者DESTINATION後面的文件存放路徑不能是自定義的一個路徑,好比主題四里面設置的當前文件目錄,不然CPack出來的安裝包都是空文件;3.License.txt只是你本身添加的信息文本,本身隨意編寫一個就好,若是沒有會在cmake的時候出現錯誤致使不經過

接下來就是日常同樣構建工程,make編譯完成以後執行CPack命令:

1. cpack -C CpackConfig.cmake  //生成二進制安裝包

2. cpack -C CpackSourceConfig.cmake //生成源碼安裝包

下面鍵入cpack -C CpackSourceConfig.cmake試一試結果

[Tino@Tino-linux demo_test]$ cpack -C CpackSourceConfig.cmake
CPack: Create package using STGZ
CPack: Install projects
CPack: - Run preinstall target for: Demo9
CPack: - Install project: Demo9
CPack: Create package
CPack: - package: /home/Tino/Documents/demo_test/Demo9-1.0.1-Linux.sh generated.
CPack: Create package using TGZ
CPack: Install projects
CPack: - Run preinstall target for: Demo9
CPack: - Install project: Demo9
CPack: Create package
CPack: - package: /home/Tino/Documents/demo_test/Demo9-1.0.1-Linux.tar.gz generated.
CPack: Create package using TZ
CPack: Install projects
CPack: - Run preinstall target for: Demo9
CPack: - Install project: Demo9
CPack: Create package
CPack: - package: /home/Tino/Documents/demo_test/Demo9-1.0.1-Linux.tar.Z generated.

執行完CPack命令以後,根目錄下會有三個文件不一樣格式的二進制包文件:Demo9-1.0.1-Linux.sh,Demo9-1.0.1-Linux.tar.gz,Demo9-1.0.1-Linux.tar.Z 這幾個文件所包含的內容是徹底一致的。咱們執行其中一個文件,此時會彈出一個交互界面,看到紅色標記處,那就是咱們編寫的License.txt文件內容,而後下面依據提示來UnPacking包文件

[Tino@Tino-linux demo_test]$ sh Demo0-1.0.1-Linux.sh
sh: Demo0-1.0.1-Linux.sh: No such file or directory
[Tino@Tino-linux demo_test]$ sh Demo9-1.0.1-Linux.sh
Demo9 Installer Version: 1.0.1, Copyright (c) Humanity
This is a self-extracting archive.
The archive will be extracted to: /home/Tino/Documents/demo_test

If you want to stop extracting, please press <ctrl-C>.
Tino 2017-1-22 CPack


Do you accept the license? [yN]: 
y
By default the Demo9 will be installed in:
  "/home/Tino/Documents/demo_test/Demo9-1.0.1-Linux"
Do you want to include the subdirectory Demo9-1.0.1-Linux?
Saying no will install in: "/home/Tino/Documents/demo_test" [Yn]: 
y

Using target directory: /home/Tino/Documents/demo_test/Demo9-1.0.1-Linux
Extracting, please wait...

Unpacking finished successfully

而後咱們瀏覽下UnPacking出來的Demo9-1.0.1-Linux文件裏面的內容

[Tino@Tino-linux demo_test]$ cd Demo9-1.0.1-Linux
[Tino@Tino-linux Demo9-1.0.1-Linux]$ ls bin include [Tino@Tino-linux Demo9-1.0.1-Linux]$ cd bin
[Tino@Tino-linux bin]$ ls Demo9 libMyFunction.a
[Tino@Tino-linux bin]$ ./Demo9 -100
./Demo9 Version 1.0
MyFunction: abs(-100) = 100

咱們所需的可執行文件就安靜的躺在裏面了,就能夠執行改程序了~

 

題外話

  CMake我就爲你們介紹到這裏了,再深刻的話博主暫時也沒有辦法,由於博主也是剛開始學習CMake,寫這篇文章最主要的目的仍是想以一個筆記的形式來加深理解、印象,也但願同是新手的朋友在檢索相關信息的時候能便利一下吧,但願對你們有點小幫助。等博主學習到必定程度以後會再次更新此文章,逐步完善內容的。

  文中如有任何錯誤不正確的地方,請你們爲我指出,以避免錯誤引導網友,謝謝啦!

  同時也要感謝那些常常寫博客的大牛們,爲我提供了那麼多好的技術資料,Thx~向他們看齊!!哈哈~爲你們多作貢獻。

  寫博客太累了,累死我~之後查閱資料的時候真的懷着感恩的心,由於他們都是耗費本身的時間、精力無償爲你們提供幫助。

 

聲明:本文沒有任何形式上的獲益,只是看了多篇博客資料加上本身的一些實踐理解,借鑑採納來爲你們服務。

 

相關連接:

CMAKE官方文檔(想要深刻全面的瞭解CMake建議去閱讀官方文檔,受益不淺)

某位大牛寫的文章http://www.hahack.com/codes/cmake/

能夠查看一些基本指令 https://my.oschina.net/zhangxu0512/blog/222741

http://m.blog.csdn.net/article/details?id=51289404

相關文章
相關標籤/搜索