參考
什么是CMake
CMake允許開發者編寫一種平臺無關的CMakeList.txt
文件來定制整個編譯流程,然后再根據目標用戶的平臺進一步生成所需的本地化Makefile
和工程文件
,如Unix的Makefile或Windows的Visual Studio工程。從而做到“Write once,run everywhere”。CMake是一個高級編譯配置工具,使用CMake作為項目架構系統的知名開源項目有VTK
、ITK
、KDE
、OpenCV
、OSG
等。
在linux
平臺下使用CMake生成Makefile并編譯的流程如下:
- 編寫CMake配置文件CMakeLists.txt
- 執行命令
cmake PATH
或者ccmake PATH
生成Makefile。PATH
是CMakeLists.txt所在的目錄。 - 使用
make
命令進行編譯
文中涉及實例地址
單個源文件
源碼地址
對于簡單的項目,只需要寫幾行代碼就可以了。例如,假設現在我們的項目中只有一個源文件 main.cc ,該程序的用途是計算一個數的指數冪。
#include <stdio.h>
#include <stdlib.h>
/**
* power - Calculate the power of number.
* @param base: Base value.
* @param exponent: Exponent value.
*
* @return base raised to the power exponent.
*/
double power(double base, int exponent)
{
int result = base;
int i;
if (exponent == 0) {
return 1;
}
for(i = 1; i < exponent; ++i){
result = result * base;
}
return result;
}
int main(int argc, char *argv[])
{
if (argc < 3){
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
double base = atof(argv[1]);
int exponent = atoi(argv[2]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result);
return 0;
}
編寫 CMakeLists.txt
首先編寫 CMakeLists.txt 文件,并保存在與 main.cc 源文件同個目錄下:
# CMake 最低版本號要求
cmake_minimum_required (VERSION 2.8)
# 項目信息
project (Demo1)
# 指定生成目標
add_executable(Demo main.cc)
CMakeLists.txt 的語法比較簡單,由命令
、注釋
和空格
組成,其中命令不區分大小寫。符號#
后面的內容被認為是注釋。命令由命令名稱
、小括號
和參數
組成,參數之間使用空格進行間隔。
對于上面的 CMakeLists.txt 文件,依次出現了幾個命令:
-
cmake_minimum_required
:指定運行此配置文件所需的 CMake 的最低版本; -
project
:參數值是Demo1
,該命令表示項目的名稱是Demo1
。 -
add_executable
: 將名為 main.cc 的源文件編譯成一個名稱為 Demo 的可執行文件。
編譯項目
之后,在當前目錄執行 cmake .
,得到 Makefile 后再使用 make
命令編譯得到 Demo1
可執行文件。
[root@localhost demo1]# cmake .
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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: /opt/shares/study/cmake/demo1
[root@localhost demo1]# make
Scanning dependencies of target Demo
[ 50%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
[root@localhost demo1]# ls
CMakeCache.txt CMakeFiles cmake_install.cmake CMakeLists.txt Demo main.cc Makefile
[root@localhost demo1]# ./Demo 5 4
5 ^ 4 is 625
多個源文件
源碼地址
上面的例子只有單個源文件。現在假如把 power
函數單獨寫進一個名為 MathFunctions.c
的源文件里,使得這個工程變成如下的形式:
[root@localhost cmake]# tree demo2
demo2
├── main.cc
├── MathFunctions.cc
└── MathFunctions.h
0 directories, 3 files
main.cc
#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"
int main(int argc, char *argv[])
{
if (argc < 3){
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
double base = atof(argv[1]);
int exponent = atoi(argv[2]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result);
return 0;
}
MathFunctions.h
#ifndef POWER_H
#define POWER_H
extern double power(double base, int exponent);
#endif
MathFunctions.cc
/**
** power - Calculate the power of number.
** @param base: Base value.
** @param exponent: Exponent value.
**
** @return base raised to the power exponent.
**/
double power(double base, int exponent)
{
int result = base;
int i;
if (exponent == 0) {
return 1;
}
for(i = 1; i < exponent; ++i){
result = result * base;
}
return result;
}
這個時候,CMakeLists.txt 可以改成如下的形式:
# CMake 最低版本號要求
cmake_minimum_required (VERSION 2.8)
# 項目信息
project (Demo2)
# 指定生成目標
add_executable(Demo main.cc MathFunctions.cc)
唯一的改動只是在 add_executable
命令中增加了一個MathFunctions.cc
源文件。這樣寫當然沒什么問題,但是如果源文件很多,把所有源文件的名字都加進去將是一件煩人的工作。更省事的方法是使用 aux_source_directory
命令,該命令會查找指定目錄下的所有源文件,然后將結果存進指定變量名。其語法如下:
aux_source_directory(<dir> <variable>)
因此,可以修改 CMakeLists.txt 如下:
# CMake 最低版本號要求
cmake_minimum_required (VERSION 2.8)
# 項目信息
project (Demo2)
# 查找當前目錄下的所有源文件
# 并將名稱保存到 DIR_SRCS 變量
aux_source_directory(. DIR_SRCS)
# 指定生成目標
add_executable(Demo ${DIR_SRCS})
這樣,CMake 會將當前目錄所有源文件的文件名賦值給變量 DIR_SRCS
,再指示變量DIR_SRCS
中的源文件需要編譯成一個名稱為 Demo 的可執行文件。
編譯項目
同上cmake && make
徹底清除cmake產生的緩存
從單個源文件編譯過程的ls
命令結果可以看出,cmake過程中會產生很多緩存(*.cmake, Makefile,CmakeCache.txt, CMakeFiles目錄),當目錄增多,這些緩存會遍布各個目錄,而CMake并沒有提供類似cmake clean
這種清理指令。
解決方法
在根部目錄下建立一個build目錄,然后在build目錄中編譯即可。
#mkdir build
#cd build
#cmake ${path}
[root@localhost build]# ls
CMakeCache.txt CMakeFiles cmake_install.cmake Demo Makefile
這樣,產生的緩存都在build目錄下了。
在下一次編譯之前,只要先刪除build下的內容即可,可以做成一個腳本,避免重復操作。
多個目錄,多個源文件
源碼地址
現在進一步將 MathFunctions.h 和 MathFunctions.cc 文件移動到 math 目錄下
[root@localhost cmake]# tree demo3
demo3
├── main.cc
└── math
├── MathFunctions.cc
└── MathFunctions.h
1 directory, 3 files
因為新增math目錄,main.cc文件修改如下:
#include <stdio.h>
#include <stdlib.h>
#include "math/MathFunctions.h"
int main(int argc, char *argv[])
{
if (argc < 3){
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
double base = atof(argv[1]);
int exponent = atoi(argv[2]);
double result = power(base, exponent);
printf("%g ^ %d is %g\n", base, exponent, result);
return 0;
}
對于這種情況,需要分別在項目根目錄 Demo3 和 math 目錄里各編寫一個 CMakeLists.txt 文件
。為了方便,我們可以先將 math 目錄里的文件編譯成靜態庫再由 main 函數調用。
根目錄中的 CMakeLists.txt :
# CMake 最低版本號要求
cmake_minimum_required (VERSION 2.8)
# 項目信息
project (Demo3)
# 查找當前目錄下的所有源文件
# 并將名稱保存到 DIR_SRCS 變量
aux_source_directory(. DIR_SRCS)
# 添加 math 子目錄
add_subdirectory(math)
# 指定生成目標
add_executable(Demo main.cc)
# 添加鏈接庫
target_link_libraries(Demo MathFunctions)
該文件添加了下面的內容: 第3行,使用命令add_subdirectory
指明本項目包含一個子目錄 math,這樣 math 目錄下的 CMakeLists.txt 文件和源代碼也會被處理 。第6行,使用命令target_link_libraries
指明可執行文件 main 需要連接一個名為 MathFunctions 的鏈接庫 。
子目錄中的 CMakeLists.txt:
# 查找當前目錄下的所有源文件
# 并將名稱保存到 DIR_LIB_SRCS 變量
aux_source_directory(. DIR_LIB_SRCS)
# 生成鏈接庫
add_library (MathFunctions ${DIR_LIB_SRCS})
在該文件中使用命令 add_library
將 src 目錄中的源文件編譯為靜態鏈接庫。
自定義編譯選項
源碼地址
CMake 允許為項目增加編譯選項,從而可以根據用戶的環境和需求選擇最合適的編譯方案。
例如,可以將 MathFunctions 庫設為一個可選的庫
,如果該選項為 ON
,就使用該庫定義的數學函數來進行運算。否則就調用標準庫中的數學函數庫。
修改CMakeLists文件
我們要做的第一步是在頂層的 CMakeLists.txt 文件中添加該選項:
# CMake 最低版本號要求
cmake_minimum_required (VERSION 2.8)
# 項目信息
project (Demo4)
#set(CMAKE_INCLUDE_CURRENT_DIR ON)
# 是否使用自己的 MathFunctions 庫
option (USE_MYMATH "Use provided math implementation" OFF)
# 是否加入 MathFunctions 庫
if (USE_MYMATH)
include_directories ("${PROJECT_SOURCE_DIR}/math")
add_subdirectory (math)
set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)
# 加入一個配置頭文件,用于處理 CMake 對源碼的設置
configure_file (
"${PROJECT_SOURCE_DIR}/config.h.in"
"${PROJECT_BINARY_DIR}/config.h"
#"${PROJECT_SOURCE_DIR}/config.h"
)
include_directories (${PROJECT_BINARY_DIR})
# 查找當前目錄下的所有源文件
# 并將名稱保存到 DIR_SRCS 變量
aux_source_directory(. DIR_SRCS)
# 指定生成目標
add_executable(Demo ${DIR_SRCS})
#if (USE_MYMATH)
target_link_libraries (Demo ${EXTRA_LIBS})
#endif (USE_MYMATH)
這里有個注意點:
原博客中,Configure_file在option及條件判斷之前,導致更改ON
或者OFF
選項不生效
其中:
- 第7行的
configure_file
命令用于加入一個配置頭文件 config.h ,這個文件由 CMake 從 config.h.in 生成,通過這樣的機制,將可以通過預定義一些參數和變量來控制代碼的生成。
- 第7行的
- 第13行的
option
命令添加了一個USE_MYMATH
選項,并且默認值為ON
。
- 第13行的
- 第17行根據
USE_MYMATH
變量的值來決定是否使用我們自己編寫的 MathFunctions 庫
- 第17行根據
修改 main.cc 文件
之后修改 main.cc 文件,讓其根據 USE_MYMATH
的預定義值來決定是否調用標準庫還是 MathFunctions 庫:
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#ifdef USE_MYMATH
#include "math/MathFunctions.h"
#else
#include <math.h>
#endif
int main(int argc, char *argv[])
{
if (argc < 3){
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
double base = atof(argv[1]);
int exponent = atoi(argv[2]);
#ifdef USE_MYMATH
printf("Now we use our own Math library. \n");
double result = power(base, exponent);
#else
printf("Now we use the standard library. \n");
double result = pow(base, exponent);
#endif
printf("%g ^ %d is %g\n", base, exponent, result);
return 0;
}
編寫 config.h.in 文件
上面的程序值得注意的是第3行,這里引用了一個 config.h 文件,這個文件預定義了 USE_MYMATH
的值。但我們并不直接編寫這個文件,為了方便從 CMakeLists.txt 中導入配置,我們編寫一個 config.h.in 文件,內容如下:
#cmakedefine USE_MYMATH
這樣 CMake 會自動根據 CMakeLists 配置文件中的設置自動生成 config.h 文件。
編譯項目
現在編譯一下這個項目,為了便于交互式的選擇該變量的值,可以使用 ccmake 2命令:
2 也可以使用
cmake -i
命令,該命令會提供一個會話式的交互式配置界面。
本地運行ccmake不生效,這里暫時先不擴展ccmake,依然用cmake和make命令來編譯。
USE_MYMATH 為 OFF
運行結果:
[root@localhost bulid]# ./Demo 3 4
Now we use the standard library.
3 ^ 4 is 81
此時 config.h 的內容為:
/* #undef USE_MYMATH */
USE_MYMATH 為 ON
運行結果:
[root@localhost bulid]# ./Demo 3 4
Now we use our own Math library.
3 ^ 4 is 81
此時 config.h 的內容為:
[root@localhost bulid]# cat config.h
#define USE_MYMATH
安裝和測試
源碼地址
CMake 也可以指定安裝規則
,以及添加測試
。這兩個功能分別可以通過在產生 Makefile 后使用 make install
和 make test
來執行。在以前的 GNU Makefile 里,你可能需要為此編寫 install 和 test 兩個偽目標和相應的規則,但在 CMake 里,這樣的工作同樣只需要簡單的調用幾條命令。
定制安裝規則
首先先在 math/CMakeLists.txt 文件里添加下面兩行:
# 指定 MathFunctions 庫的安裝路徑
install (TARGETS MathFunctions DESTINATION bin)
install (FILES MathFunctions.h DESTINATION include)
指明 MathFunctions 庫的安裝路徑。之后同樣修改根目錄的 CMakeLists 文件,在末尾添加下面幾行:
# 指定安裝路徑
install (TARGETS Demo DESTINATION bin)
install (FILES "${PROJECT_BINARY_DIR}/config.h"
DESTINATION include)
通過上面的定制,生成的 Demo 文件和 MathFunctions 函數庫 libMathFunctions.o 文件將會被復制到/usr/local/bin
中,而 MathFunctions.h 和生成的 config.h 文件則會被復制到 /usr/local/include
中。我們可以驗證一下:
[root@localhost build]# cmake ../
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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: /opt/shares/study/cmake/demo5/build
[root@localhost build]# make
Scanning dependencies of target MathFunctions
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/MathFunctions.cc.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
Scanning dependencies of target Demo
[ 75%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
[root@localhost build]# make install
[ 50%] Built target MathFunctions
[100%] Built target Demo
Install the project...
-- Install configuration: ""
-- Installing: /usr/local/bin/Demo
-- Installing: /usr/local/include/config.h
-- Installing: /usr/local/bin/libMathFunctions.a
-- Installing: /usr/local/include/MathFunctions.h
[root@localhost build]# ls /usr/local/bin/
Demo iperf3 libMathFunctions.a thrift
[root@localhost build]# ls /usr/local/include/
config.h iperf_api.h MathFunctions.h thrift
順帶一提的是,這里的
/usr/local/
是默認安裝到的根目錄,可以通過修改CMAKE_INSTALL_PREFIX
變量的值來指定這些文件應該拷貝到哪個根目錄
為工程添加測試
添加測試同樣很簡單。CMake 提供了一個稱為 CTest 的測試工具。我們要做的只是在項目根目錄的 CMakeLists 文件中調用一系列的add_test
命令。
# 啟用測試
enable_testing()
# 測試程序是否成功運行
add_test (test_run Demo 5 2)
# 測試幫助信息是否可以正常提示
add_test (test_usage Demo)
set_tests_properties (test_usage
PROPERTIES PASS_REGULAR_EXPRESSION "Usage: .* base exponent")
# 測試 5 的平方
add_test (test_5_2 Demo 5 2)
set_tests_properties (test_5_2
PROPERTIES PASS_REGULAR_EXPRESSION "is 25")
# 測試 10 的 5 次方
add_test (test_10_5 Demo 10 5)
set_tests_properties (test_10_5
PROPERTIES PASS_REGULAR_EXPRESSION "is 100000")
# 測試 2 的 10 次方
add_test (test_2_10 Demo 2 10)
set_tests_properties (test_2_10
PROPERTIES PASS_REGULAR_EXPRESSION "is 1024")
上面的代碼包含了四個測試。第一個測試test_run
用來測試程序是否成功運行并返回 0 值。剩下的三個測試分別用來測試 5 的 平方、10 的 5 次方、2 的 10 次方是否都能得到正確的結果。其中 PASS_REGULAR_EXPRESSION
用來測試輸出是否包含后面跟著的字符串。
讓我們看看測試的結果:
[root@localhost build]# make test
Running tests...
Test project /opt/shares/study/cmake/demo5/build
Start 1: test_run
1/5 Test #1: test_run ......................... Passed 0.00 sec
Start 2: test_usage
2/5 Test #2: test_usage ....................... Passed 0.00 sec
Start 3: test_5_2
3/5 Test #3: test_5_2 ......................... Passed 0.00 sec
Start 4: test_10_5
4/5 Test #4: test_10_5 ........................ Passed 0.00 sec
Start 5: test_2_10
5/5 Test #5: test_2_10 ........................ Passed 0.00 sec
100% tests passed, 0 tests failed out of 5
Total Test time (real) = 0.01 sec
如果要測試更多的輸入數據,像上面那樣一個個寫測試用例未免太繁瑣。這時可以通過編寫宏來實現:
enable_testing()
# 定義一個宏,用來簡化測試工作
macro (do_test arg1 arg2 result)
add_test (test_${arg1}_${arg2} Demo ${arg1} ${arg2})
set_tests_properties (test_${arg1}_${arg2}
PROPERTIES PASS_REGULAR_EXPRESSION ${result})
endmacro (do_test)
# 使用該宏進行一系列的數據測試
do_test (5 2 "is 25")
do_test (10 5 "is 100000")
do_test (2 10 "is 1024")
[root@localhost build]# make test
Running tests...
Test project /opt/shares/study/cmake/demo5/build
Start 1: test_5_2
1/3 Test #1: test_5_2 ......................... Passed 0.00 sec
Start 2: test_10_5
2/3 Test #2: test_10_5 ........................ Passed 0.00 sec
Start 3: test_2_10
3/3 Test #3: test_2_10 ........................ Passed 0.00 sec
100% tests passed, 0 tests failed out of 3
Total Test time (real) = 0.01 sec
關于 CTest 的更詳細的用法可以通過 man 1 ctest
參考 CTest 的文檔
支持 gdb
讓 CMake 支持 gdb 的設置也很容易,只需要指定 Debug
模式下開啟-g
選項:
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
之后可以直接對生成的程序使用 gdb 來調試。
添加環境檢查
源碼地址
有時候可能要對系統環境做點檢查,例如要使用一個平臺相關的特性的時候。在這個例子中,我們檢查系統是否自帶 pow 函數。如果帶有 pow 函數,就使用它;否則使用我們定義的 power 函數。
添加 CheckFunctionExists 宏
首先在頂層 CMakeLists 文件中添加 CheckFunctionExists.cmake
宏,并調用 check_function_exists
命令測試鏈接器是否能夠在鏈接階段找到 pow
函數。
# 檢查系統是否支持 pow 函數
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
set (CMAKE_REQUIRED_INCLUDES math.h)
set (CMAKE_REQUIRED_LIBRARIES m)
check_function_exists (pow HAVE_POW)
將上面這段代碼放在configure_file
命令前。
預定義相關宏變量
接下來修改 config.h.in 文件,預定義相關的宏變量。
// does the platform provide pow function?
#cmakedefine HAVE_POW
在代碼中使用宏和函數
最后一步是修改 main.cc ,在代碼中使用宏和函數:
#ifdef HAVE_POW
printf("Now we use the standard library. \n");
double result = pow(base, exponent);
#else
printf("Now we use our own Math library. \n");
double result = power(base, exponent);
#endif
添加版本號
Demo7
給項目添加和維護版本號是一個好習慣,這樣有利于用戶了解每個版本的維護情況,并及時了解當前所用的版本是否過時,或是否可能出現不兼容的情況。
首先修改頂層 CMakeLists 文件,在 project 命令之后加入如下兩行:
set (Demo_VERSION_MAJOR 1)
set (Demo_VERSION_MINOR 0)
分別指定當前的項目的主版本號和副版本號。
之后,為了在代碼中獲取版本信息,我們可以修改 config.h.in 文件,添加兩個預定義變量:
// the configured options and settings for Tutorial
#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@
這樣就可以直接在代碼中打印版本信息了:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "config.h"
#include "math/MathFunctions.h"
int main(int argc, char *argv[])
{
if (argc < 3){
// print version info
printf("%s Version %d.%d\n",
argv[0],
Demo_VERSION_MAJOR,
Demo_VERSION_MINOR);
printf("Usage: %s base exponent \n", argv[0]);
return 1;
}
double base = atof(argv[1]);
int exponent = atoi(argv[2]);
#if defined (HAVE_POW)
printf("Now we use the standard library. \n");
double result = pow(base, exponent);
#else
printf("Now we use our own Math library. \n");
double result = power(base, exponent);
#endif
printf("%g ^ %d is %g\n", base, exponent, result);
return 0;
}
[root@localhost build]# ./Demo
./Demo Version 1.0
Usage: ./Demo base exponent
生成安裝包
本節將學習如何配置生成各種平臺上的安裝包,包括二進制安裝包和源碼安裝包。為了完成這個任務,我們需要用到CPack
,它同樣也是由 CMake 提供的一個工具,專門用于打包。
首先在頂層的 CMakeLists.txt 文件尾部添加下面幾行:
# 構建一個 CPack 安裝包
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE
"${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}")
include (CPack)
上面的代碼做了以下幾個工作:
- 導入
InstallRequiredSystemLibraries
模塊,以便之后導入 CPack 模塊; - 設置一些 CPack 相關變量,包括
版權信息
和版本信息
,其中版本信息用了上一節定義的版本號; - 導入 CPack 模塊。
接下來的工作是像往常一樣構建工程,并執行 cpack 命令。
生成二進制安裝包:
cpack -C CPackConfig.cmake
生成源碼安裝包
cpack -C CPackSourceConfig.cmake
我們可以試一下。在生成項目后,執行 cpack -C CPackConfig.cmake
命令:
[root@localhost build]# make
Scanning dependencies of target Demo
[ 50%] Building CXX object CMakeFiles/Demo.dir/main.cc.o
[100%] Linking CXX executable Demo
[100%] Built target Demo
[root@localhost build]# cpack -C CPackConfig.cmake
CPack: Create package using STGZ
CPack: Install projects
CPack: - Run preinstall target for: Demo4
CPack: - Install project: Demo4
CPack: Create package
CPack: - package: /opt/shares/study/cmake/demo8/build/Demo4-1.0.1-Linux.sh generated.
CPack: Create package using TGZ
CPack: Install projects
CPack: - Run preinstall target for: Demo4
CPack: - Install project: Demo4
CPack: Create package
CPack: - package: /opt/shares/study/cmake/demo8/build/Demo4-1.0.1-Linux.tar.gz generated.
CPack: Create package using TZ
CPack: Install projects
CPack: - Run preinstall target for: Demo4
CPack: - Install project: Demo4
CPack: Create package
CPack: - package: /opt/shares/study/cmake/demo8/build/Demo4-1.0.1-Linux.tar.Z generated.
此時會在該目錄下創建 3 個不同格式的二進制包文件:
[root@localhost build]# ls | grep Demo4
Demo4-1.0.1-Linux.sh
Demo4-1.0.1-Linux.tar.gz
Demo4-1.0.1-Linux.tar.Z
這 3 個二進制包文件所包含的內容是完全相同的。我們可以執行其中一個。此時會出現一個由 CPack 自動生成的交互式安裝界面:
[root@localhost build]# sh Demo4-1.0.1-Linux.sh
Demo4 Installer Version: 1.0.1, Copyright (c) Humanity
This is a self-extracting archive.
The archive will be extracted to: /opt/shares/study/cmake/demo8/build
If you want to stop extracting, please press <ctrl-C>.
license
Do you accept the license? [yN]:
y
By default the Demo4 will be installed in:
"/opt/shares/study/cmake/demo8/build/Demo4-1.0.1-Linux"
Do you want to include the subdirectory Demo4-1.0.1-Linux?
Saying no will install in: "/opt/shares/study/cmake/demo8/build" [Yn]:
y
Using target directory: /opt/shares/study/cmake/demo8/build/Demo4-1.0.1-Linux
Extracting, please wait...
Unpacking finished successfully
完成后提示安裝到了 Demo8-1.0.1-Linux 子目錄中,我們可以進去執行該程序:
[root@localhost build]# ./Demo4-1.0.1-Linux/bin/Demo 3 4
Now we use the standard library.
3 ^ 4 is 81
關于 CPack 的更詳細的用法可以通過 man 1 cpack
參考 CPack 的文檔。
將其他平臺的項目遷移到 CMake
CMake 可以很輕松地構建出在適合各個平臺執行的工程環境。而如果當前的工程環境不是 CMake ,而是基于某個特定的平臺,是否可以遷移到 CMake 呢?答案是可能的。下面針對幾個常用的平臺,列出了它們對應的遷移方案。
autotools
- am2cmake 可以將 autotools 系的項目轉換到 CMake,這個工具的一個成功案例是 KDE 。
- Alternative Automake2CMake 可以轉換使用 automake 的 KDevelop 工程項目。
- Converting autoconf tests
qmake
- qmake converter 可以轉換使用 QT 的 qmake 的工程。
Visual Studio
-
vcproj2cmake.rb 可以根據 Visual Studio 的工程文件(后綴名是
.vcproj
或.vcxproj
)生成 CMakeLists.txt 文件。 - vcproj2cmake.ps1 vcproj2cmake 的 PowerShell 版本。
- folders4cmake 根據 Visual Studio 項目文件生成相應的 “source_group” 信息,這些信息可以很方便的在 CMake 腳本中使用。支持 Visual Studio 9/10 工程文件。
CMakeLists.txt 自動推導
- gencmake 根據現有文件推導 CMakeLists.txt 文件。
- CMakeListGenerator 應用一套文件和目錄分析創建出完整的 CMakeLists.txt 文件。僅支持 Win32 平臺。