CMake 使用教程

資源

本文檔翻譯自官方 cmake turorial 。更新日期:2018年9月27日蛉签。譯者這里以 windows 平臺為例胡陪,介紹了在 Windows 平臺下配合 VS2017 的使用方法柠座。

雖然本文是官方的入門教程,但本教程并不是面向從未使用過 CMake 的用戶妈经。本文并不會對使用到的命令進行一一解釋,所以在閱讀本文之前用戶應該對 CMake 有一些了解和接觸吹泡。

下面是一個一步步的教程包含了使用 CMake 構建系統(tǒng)的常用情況。下面介紹的許多內容都在 Mastering CMake 一書中作為單獨的議題介紹過了经瓷,但在一個樣例項目中演示如何將它們結合在一起同樣是是否有用的爆哑。本教程可以在 CMake 源文件中的 Tests/Tutorial 目錄下找到。每一個步驟都有它們自己的子文件夾了嚎,包含了對應步驟完整的教程源碼泪漂。

通過查看 cmake-buildsystemcmake-language 手冊的介紹章節(jié)可以對 CMake 的設計理念和源碼結構組織有一個整體的了解。

基礎的構建步驟(步驟1)

一個最常用的基礎項目是從源碼中構建一個可執(zhí)行文件歪泳。對于一個簡單的項目兩行 CMakeLists.txt 文件就能搞定:

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

上面的例子中使用了小寫的命令,事實上露筒,CMakeLists.txt 文件并不區(qū)分命令的大小寫呐伞。tutorial.cxx 源碼是用來計算一個數(shù)的算數(shù)平方根,下面是其一個簡單的版本:

// 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[])
{
  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);
  system("pause");
  return 0;
}

譯者這里在 16 行附加了一行 system("pause"); 慎式,是為了程序執(zhí)行完畢后不會立刻關閉窗口伶氢。后面的代碼的示例中并不會再添加此行,如果用戶需要暫停的話瘪吏,可以在自己的代碼中加入該行癣防。

添加版本號和配置頭文件

第一個要添加的特性就是給我們的可執(zhí)行文件和項目提供一個版本號。你可以在你的源碼之外做到這一點掌眠,在 CMakeLists.txt 文件中做這些會更加靈活蕾盯。為了添加一個版本號我們修改我們的 CMakeList.txt 文件如下:

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

因為配置文件會被寫入到生成路徑(binary tree) 中,所以我們必須將該文件夾添加到頭文件搜索路徑中蓝丙。接下來我們在源碼中創(chuàng)建一個包含以下內容的 TutorialConfig.h.in 文件:

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

當 CMake 配置這個頭文件的時候级遭,@Tutorial_VERSION_MAJOR@ 和 @ Tutorial_VERSION_MINOR@ 就會用CMakeLists.txt 文件中對應的值替換。接下來我們修改 tutorial.cxx 源碼包含配置頭文件并使用版本號渺尘,修改后的源碼如下:

// 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[])
{
  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;
}

構建項目并執(zhí)行文件

官方并沒有給出如何構建項目挫鸽,這里以 VS 為例介紹如何構建以上項目并編譯執(zhí)行。在該目錄下面建立 build 文件夾鸥跟,并新建 run.cmd 文件丢郊,編寫內容如下:

echo off
echo build:
cmake -G "Visual Studio 15 2017 Win64" ..
echo compile:
devenv Tutorial.sln /build "Debug|x64"
echo run:
start ./Debug/Tutorial.exe %1

上面腳本中 echo命令主要是用來輸出提示信息,可以忽略。剩下一共有三行代碼迅诬。

第3行代碼為使用 CMake 構建工程文件.-G 參數(shù)用來指定編譯器侈贷,如果不寫這里會找到一個默認的編譯器等脂。我這里默認的編譯器就是 VS2017,但是默認構建的程序為 32 位程序搏屑,我這里顯示的指定使用 VS2017 構建 64 位程序辣恋。

第5行代碼是使用命令行的形式編譯 VS 的 .sln 文件模软。關于命令行構建 VS 項目這里不做過多介紹,有興趣可以參考微軟官方給出的 Devenv command line switches携狭。當然我們也可以使用 VS 打開 .sln 文件逛腿,然后手動點擊 生成 单默。

第7行代碼為運行程序忘瓦。

添加一個庫文件(步驟2)

現(xiàn)在我們將會給我們的項目添加一個庫文件。這個庫文件包含了我們自己實現(xiàn)的開方運算枚抵∑。可執(zhí)行文件使用這個庫替代編譯器提供的標準開方運算苦锨。本教程中我們將其放到 MathFunctions 文件夾下,該文件夾下還有一個包含下面一行代碼的 CMakeLists.txt 文件拉庶。

add_library(MathFunctions mysqrt.cxx)

mysqrt.cxx 文件只有一個名為 mysqrt 的函數(shù),其提供了和標準庫 sqrt 相同的功能吉捶。內容如下(官網(wǎng)官方例程中可以找到):

#include "MathFunctions.h"
#include <stdio.h>

// a hack square root calculation using simple operations
double mysqrt(double x)
{
  if (x <= 0) {
    return 0;
  }

  double result;
  double delta;
  result = x;

  // do ten iterations
  int i;
  for (i = 0; i < 10; ++i) {
    if (result <= 0) {
      result = 0.1;
    }
    delta = x - (result * result);
    result = result + 0.5 * delta / result;
    fprintf(stdout, "Computing sqrt of %g to be %g\n", x, result);
  }
  return result;
}

對應的頭文件為 MathFunction.h皆尔,其內容如下:

double mysqrt(double x);

為了構建并使用新的庫文件慷蠕,我們需要在頂層 CMakeList.txt 文件添加 add_subdirectory 語句。我們需要添加額外的頭文件包含路徑澎现,以便將包含函數(shù)原型的 MathFunctions/MathFunctions.h 頭文件包含進來每辟。最后我們還需要給可執(zhí)行文件添加庫影兽。最終頂層 CMakeList.txt 文件的最后幾行如下所示:

include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
add_subdirectory (MathFunctions) 
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial MathFunctions)

現(xiàn)在我們考慮將 MathFunctions 庫作為一個可選項峻堰。雖然在這里并沒有什么必要捐名,但是如果庫文件很大或者庫文件依賴第三方庫你可能就希望這么做了闹击。首先先在頂層 CMakeLists.txt 文件添加一個選項:

# should we use our own math functions?
option (USE_MYMATH 
        "Use tutorial provided math implementation" ON) 

這個選項會在 CMake GUI 中顯示并會將默認值設置為 ON赏半,用戶可以根據(jù)需求修改該值。這個設置會本保存下來拂酣,所以用戶無需在每次運行 CMake 時都去設置仲义。接下來就是將構建和連接 MathFunctions 設置為可選項剑勾。修改頂層的 CMakeLists.txt 文件如下所示:

# add the MathFunctions library?
#
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial  ${EXTRA_LIBS})

這里使用 USE_MYMATH 來決定是否編譯并使用 MathFunctions 虽另。注意收集可執(zhí)行文件的可選連接庫所使用的變量(這里為 EXTRA_LIBS)的使用方法饺谬。這種方法在保持有許多可選組件的大型項目整潔時經(jīng)常使用募寨。對應的我們修改源碼如下:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
#ifdef USE_MYMATH
#include "MathFunctions.h"
#endif
 
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]);
 
#ifdef USE_MYMATH
  double outputValue = mysqrt(inputValue);
#else
  double outputValue = sqrt(inputValue);
#endif
 
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}

在源碼中我們同樣使用了 USE_MYMATH 宏绪商。這個宏由 CMake 通過在配置文件 TutorialConfig.h 添加以下代碼傳遞給源碼:

#cmakedefine USE_MYMATH

構建格郁、編譯和運行使用的代碼和上一節(jié)相同。

安裝和測試(步驟3)

下一步我們將給我們的項目添加安裝規(guī)則和測試锣尉。安裝規(guī)則簡單明了自沧。對于 MathFunctions 庫的安裝树瞭,我們通過在 MathFunction 的 CMakeLists.txt 文件中添加以下兩行來設置其庫和頭文件的安裝。

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

對于本文這個應用通過在頂層 CMakeLists.txt 添加以下內容來安裝可執(zhí)行文件和配置的頭文件:

# add the install targets
install (TARGETS Tutorial DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"        
         DESTINATION include)

以上就是安裝的全部步驟⌒①耍現(xiàn)在你應該可以編譯本教程了衣盾。輸入 make install(或在 IDE 中編譯 install 項目)爷抓,對應的頭文件蓝撇、庫文件和可執(zhí)行文件就會被安裝。CMake 的 CMAKE_INSTALL_PREFIX 參數(shù)可以指定安裝文件的根目錄(之前還可以加上 -D 參數(shù)据悔,具體意義可以參考 what does the parameter "-D" mean)极颓。

添加測試過程同樣簡單明了。在頂層 CMakeLists.txt 文件的最后我們可以添加一個基礎測試數(shù)據(jù)來驗證該應用程序是否正常運行兵琳。

include(CTest)

# does the application run
add_test (TutorialRuns Tutorial 25)
# does it sqrt of 25
add_test (TutorialComp25 Tutorial 25)
set_tests_properties (TutorialComp25 PROPERTIES PASS_REGULAR_EXPRESSION "25 is 5")
# does it handle negative numbers
add_test (TutorialNegative Tutorial -25)
set_tests_properties (TutorialNegative PROPERTIES PASS_REGULAR_EXPRESSION "-25 is 0")
# does it handle small numbers
add_test (TutorialSmall Tutorial 0.0001)
set_tests_properties (TutorialSmall PROPERTIES PASS_REGULAR_EXPRESSION "0.0001 is 0.01")
# does the usage message work?
add_test (TutorialUsage Tutorial)
set_tests_properties (TutorialUsage PROPERTIES PASS_REGULAR_EXPRESSION "Usage:.*number")

在編譯完成之后躯肌,我們可以運行 "ctest" 命令行工具來執(zhí)行測試破衔。第一個測試簡單的驗證了程序是否工作晰筛,是否有嚴重錯誤并且返回0.這是 Ctest 測試的基礎读第。接下來的一些測試都使用了 PASS_REGULAR_EXPRESSION 測試屬性(正則表達式)來驗證輸出中是否包含了特定的字符串。這里驗證開方是否正確并且在計算錯誤時輸出輸出對應信息父泳。如果你希望添加很多的測試來測試不同的輸入值惠窄,你可以考慮定義一個像下面這樣的宏:

#define a macro to simplify adding tests, then use it
macro (do_test arg result)
  add_test (TutorialComp${arg} Tutorial ${arg})
  set_tests_properties (TutorialComp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)
 
# do a bunch of result based tests
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")

每一次調用 do_test 就會根據(jù)指定的信息生成一個新的測試睬捶。

該步驟對應的 build 文件夾下的構建和運行腳本 run.cmd 內容如下:

echo off
echo build:
cmake -G "Visual Studio 15 2017 Win64" -DCMAKE_INSTALL_PREFIX=D:\project\cpp\cmake\tutorial\step3\install ..
echo compile:
devenv Tutorial.sln /build "Debug|x64"
echo install:
devenv Tutorial.sln /build "Debug|x64" /project INSTALL
echo test:
devenv Tutorial.sln /build "Debug|x64" /project RUN_TESTS

安裝位置根據(jù)自己的需要進行調整近刘。

添加系統(tǒng)自檢(步驟 4)

接下來我們考慮給我們的項目添加一些取決于目標平臺是否有一些特性的代碼臀晃。這里我們將添加一些取決于目標平臺是否有 log 和 exp 函數(shù)的代碼徽惋。當然對于大多數(shù)平臺都會有這些函數(shù),但這里我們認為這并不常見踢京。如果平臺有 log 函數(shù)我們將在 mysqrt 函數(shù)中使用它計算平方根。我們首先在頂層 CMakeLists.txt 文件中使用 CheckFunctionExists 宏測試這些函數(shù)是否可用黔帕,代碼如下:

# does this system provide the log and exp functions?
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)

接下來我們修改 TutorialConfig.h.in 文件定義一些宏以表示 CMake 是否在平臺上找到這些函數(shù):

// does the platform provide exp and log functions?
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP

一定要在使用 configure_file 生成 TutorialConfig.h 之前測試 log 和 exp成黄。因為 configure_file 命令會立刻使用當前 CMake 的設置配置文件奋岁。最后根據(jù) log 和 exp 是否在我們的平臺上可用我們給 mysqrt 函數(shù)提供一個可選的實現(xiàn)闻伶,代碼如下:

// if we have both log and exp then use them
#if defined (HAVE_LOG) && defined (HAVE_EXP)
  result = exp(log(x)*0.5);
#else // otherwise use an iterative approach
  . . .

添加一個生成的文件和生成器(步驟 5)

在這一章節(jié)我們將會展示如何在構建一個應用的過程中添加一個生成的源文件够话。在本例中我們將創(chuàng)建一個預先計算的平方根表作為構建過程的一部分更鲁,然后將其編譯到我們的應用中澡为。為了做到這一點我們首先需要一個能產(chǎn)生這張表的程序。在 MathFunctions 文件夾下創(chuàng)建一個新的名為 MakeTable.cxx 的文件顶别,內容如下:

// A simple program that builds a sqrt table 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main (int argc, char *argv[])
{
  int i;
  double result;
 
  // make sure we have enough arguments
  if (argc < 2)
    {
    return 1;
    }
  
  // open the output file
  FILE *fout = fopen(argv[1],"w");
  if (!fout)
    {
    return 1;
    }
  
  // create a source file with a table of square roots
  fprintf(fout,"double sqrtTable[] = {\n");
  for (i = 0; i < 10; ++i)
    {
    result = sqrt(static_cast<double>(i));
    fprintf(fout,"%g,\n",result);
    }
 
  // close the table with a zero
  fprintf(fout,"0};\n");
  fclose(fout);
  return 0;
}

注意到這張表使用 C++ 代碼生成且文件的名字通過輸入?yún)?shù)指定驯绎。下一步通過在 MathFunctions 的 CMakeLists.txt 中添加合適的代碼來構建 MakeTable 可執(zhí)行文件剩失,并將它作為構建的一部分運行册着。只需要一點代碼就能實現(xiàn)這個功能:

# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
 
# add the command to generate the source code
add_custom_command (
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  DEPENDS MakeTable
  )
 
# add the binary tree directory to the search path for 
# include files
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
 
# add the main library
add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h  )

首先添加的 MakeTable 可執(zhí)行文件和其它可執(zhí)行文件相同演熟。接下來我們添加一個自定義的命令來指定如何通過運行 MakeTable 生成 Table.h 文件司顿。接下來我們必須讓 CMake 知道 mysqrt.cxx 依賴于生成的 Table.h 文件兄纺。這一點通過將生成的 Table.h 文件添加到 MathFunctions 庫的源文件列表實現(xiàn)估脆。我們同樣必須將當前二進制文件路徑添加到包含路徑中获三,以保證 Table.h 文件被找到并被 mysqrt.cxx 包含疙教。該項目在構建時會首先構建 MakeTable 可執(zhí)行文件贞谓。接下來會運行該可執(zhí)行文件并生成 Table.h 文件。最后它將會編譯包含 Table.h 的 mysqrt.cxx 文件并生成 MathFunctions 庫祟同。此時包含了所有我們添加的特性的頂層 CMakeLists.txt 文件應該像下面這樣:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
include(CTest)
 
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
 
# does this system provide the log and exp functions?
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
 
check_function_exists (log HAVE_LOG)
check_function_exists (exp HAVE_EXP)
 
# should we use our own math functions
option(USE_MYMATH 
  "Use tutorial provided math implementation" ON)
 
# 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 MathFunctions library?
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions")
  add_subdirectory (MathFunctions)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
 
# add the executable
add_executable (Tutorial tutorial.cxx)
target_link_libraries (Tutorial  ${EXTRA_LIBS})
 
# add the install targets
install (TARGETS Tutorial DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/TutorialConfig.h"        
         DESTINATION include)
 
# does the application run
add_test (TutorialRuns Tutorial 25)
 
# does the usage message work?
add_test (TutorialUsage Tutorial)
set_tests_properties (TutorialUsage
  PROPERTIES 
  PASS_REGULAR_EXPRESSION "Usage:.*number"
  )
 
 
#define a macro to simplify adding tests
macro (do_test arg result)
  add_test (TutorialComp${arg} Tutorial ${arg})
  set_tests_properties (TutorialComp${arg}
    PROPERTIES PASS_REGULAR_EXPRESSION ${result}
    )
endmacro (do_test)
 
# do a bunch of result based tests
do_test (4 "4 is 2")
do_test (9 "9 is 3")
do_test (5 "5 is 2.236")
do_test (7 "7 is 2.645")
do_test (25 "25 is 5")
do_test (-25 "-25 is 0")
do_test (0.0001 "0.0001 is 0.01")

TutorialConfig.h 文件如下:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
#cmakedefine USE_MYMATH
 
// does the platform provide exp and log functions?
#cmakedefine HAVE_LOG
#cmakedefine HAVE_EXP

最后 MathFunctions 的 CMakeLists.txt 文件如下:

# first we add the executable that generates the table
add_executable(MakeTable MakeTable.cxx)
# add the command to generate the source code
add_custom_command (
  OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  DEPENDS MakeTable
  COMMAND MakeTable ${CMAKE_CURRENT_BINARY_DIR}/Table.h
  )
# add the binary tree directory to the search path 
# for include files
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
 
# add the main library
add_library(MathFunctions mysqrt.cxx ${CMAKE_CURRENT_BINARY_DIR}/Table.h)
 
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)

構造一個安裝器(步驟 6)

接下來假設我們想將我們的項目發(fā)布給其他人以便供他們使用窖贤。我們想提供在不同平臺上的二進制文件和源碼的發(fā)布版本赃梧。這一點和我們在之前安裝和測試章節(jié)(步驟3)略有不同授嘀,步驟三安裝的二進制文件是我們從源碼構建的。這里我們將構建一個支持二進制文件安裝的安裝包和可以在 cygwin,debian,RPMs 等中被找到的安裝管理特性览闰。為了實現(xiàn)這一點我們將使用 CPack 來創(chuàng)建在 Packaging with CPack 章節(jié)中介紹過的平臺特定安裝器(platform specific installers)焕济。我們需要在頂層 CMakeLists.txt 文件添加以下幾行內容:

# build a CPack driven installer package
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE  
     "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${Tutorial_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${Tutorial_VERSION_MINOR}")
include (CPack)

首先我們添加了 InstallRequiredSystemLibraries。該模塊會包含我們項目在當前平臺所需的所有運行時庫(runtime libraries)逊拍。接下來我們設置了一些 CPack 變量來指定我們項目的許可文件和版本信息。版本信息使用我們在之前設置的內容芯丧。最后我們包含 CPack 模塊,它會使用這些變量和其它你安裝一個應用程序所需的系統(tǒng)屬性谴咸。

接下來就是正常編譯你的項目然后使用 CPack 運行它骗露,為了編譯二進制發(fā)布版本你需要運行:

cpack --config CPackConfig.cmake

創(chuàng)建一個源文件發(fā)布版本你應該使用下面命令:

cpack --config CPackSourceConfig.cmake

Windows 平臺下CMake 默認會使用 NSIS 創(chuàng)建安裝包萧锉,因此我們在執(zhí)行上面命令前需要安裝該軟件。當然我們也可以使用 WiX 包安裝工具叶洞,只需要在 include(CPack) 之前加上 set(CPACK_GENERATOR WIX) 即可衩辟。安裝包工具的選擇和對比可以參考 NISS Vs WiX Vs AnyOther Installation Package 艺晴。

添加表盤工具(Dashboard)支持(步驟7)

添加將我們測試結果提交到儀表盤的功能非常簡單叶雹。在本教程的之前步驟中我們已經(jīng)給我們的項目定義了一些測試折晦。我們只需要運行這些測試然后提交到儀表盤即可满着。為了支持儀表盤功能我們需要在頂層 CMakeLists.txt 文件中增加 CTest 模塊风喇。

# enable dashboard scripting
include (CTest)

我們同樣可以創(chuàng)建一個 CTestConfig.cmake 文件來在表盤工具中指定本項目的名字魂莫。

set (CTEST_PROJECT_NAME "Tutorial")

CTest 會在運行時讀取該文件。你可以在你的項目上運行 CMake 來創(chuàng)建一個簡單的儀表盤潭兽,切換目錄到二進制文件夾下山卦,然后運行 ctest -DExperimental.你儀表盤的運行結果會上傳到 Kitware 的公共儀表盤上 這里账蓉。

如果需要上傳的話還需要設置 Drop site 逾一,具體細節(jié)可以參考官方的 ctest(1)

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末归敬,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鄙早,更是在濱河造成了極大的恐慌汪茧,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件限番,死亡現(xiàn)場離奇詭異舱污,居然都是意外死亡,警方通過查閱死者的電腦和手機弥虐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門扩灯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人霜瘪,你說我怎么就攤上這事珠插∧沓牛” “怎么了?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長犁河。 經(jīng)常有香客問我耕魄,道長缠局,這世上最難降的妖魔是什么狭园? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任绎谦,我火速辦了婚禮,結果婚禮上冤留,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好州既,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布蚌卤。 她就那樣靜靜地躺著逊彭,像睡著了一般避矢。 火紅的嫁衣襯著肌膚如雪卸勺。 梳的紋絲不亂的頭發(fā)上曙求,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天芽淡,我揣著相機與錄音富稻,去河邊找鬼或杠。 笑死认境,一個胖子當著我的面吹牛,可吹牛的內容都是我干的硅急。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼吩屹,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起厌衙,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤喻杈,失蹤者是張志新(化名)和其女友劉穎瓷们,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體攒钳,經(jīng)...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年揩页,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡饿序,死狀恐怖原探,靈堂內的尸體忽然破棺而出离唬,到底是詐尸還是另有隱情裸诽,我是刑警寧澤嫂用,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布往弓,位于F島的核電站,受9級特大地震影響蔑担,放射性物質發(fā)生泄漏鸟缕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一畴蒲、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦辽旋、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浓利。三九已至,卻和暖如春调违,著一層夾襖步出監(jiān)牢的瞬間且轨,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留唆香,地道東北人碑宴。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓整以,卻偏偏與公主長得像,于是被迫代替她去往敵國和親涩金。 傳聞我的和親對象是個殘疾皇子芹务,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內容