【cmake系列使用教程】linux
cmake使用教程(三)-安裝、測試、系統自檢github
cmake使用教程(四)-文件生成器macos
cmake使用教程(五)-cpack生成安裝包ubuntu
cmake使用教程(六)-蛋疼的語法centos
cmake使用教程(八)-macro和functionide
這個系列的文章翻譯自官方cmake教程:cmake tutorial。post
示例程序地址:github.com/rangaofei/t…
不會僅僅停留在官方教程。本人做爲一個安卓開發者,實在是沒有linux c程序開發經驗,望大佬們海涵。教程是在macos下完成,大部分linux我也測試過,有特殊說明的我會標註出來。本教程基於cmake-3.10.2,同時認爲你已經安裝好cmake。
一個最基本的CmakeLists.txt文件最少須要包含如下三行:
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)
複製代碼
注意:cmake的語法支持大小、小寫和大小寫混合上邊的代碼中咱們使用的cmake語法是小寫的.
cmake_minimum_required
CMAKE_MINIMUM_REQUIRED
cmake_MINUMUM_required
複製代碼
上面三種寫法是相同的,注意,只有系統指令是不區分大小寫的,可是變量和字符串是區分大小寫的。
建立一個tutorial.cxx文件,用來計算一個數字的平方根,內容以下:
// 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);
return 0;
}
複製代碼
這樣就完成一個最簡單的cmake程序。
用cmake來編譯這段代碼,進入命令行執行內部構建命令(後邊會講外部構建):
cmake .
複製代碼
這是輸出一系列的log信息
-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/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: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/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: /Users/saka/Desktop/Tutorial/Step1
複製代碼
同時生成了三個文件CMakeCache.txt
、Makefile
、cmake_install.cmake
和一個文件夾CmakeFiles
,而後執行
make
複製代碼
便可生成可執行程序Tutorial
。在ubuntu或者centos上可能會提示找不到math.h
文件,這時候咱們須要在cmakeLists.txt文件中最後添加
target_link_libraries(Tutorial apue.a)
複製代碼
而後從新編譯便可。須要刪除剛纔生成的額外的文件。
下面講解如何爲程序添加版本號和帶有使用版本號的頭文件。
set(KEY VALUE)
接受兩個參數,用來聲明變量。在camke語法中使用KEY
並不能直接取到VALUE
,必須使用${KEY}
這種寫法來取到VALUE
。
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)
複製代碼
配置文件將會被寫入到可執行文件目錄下,因此咱們的項目必須包含這個文件夾來使用這些配置頭文件。咱們須要在工程目錄下新建一個TutorialConfig.h.in
,內容以下:
// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
複製代碼
上面的代碼中的@Tutorial_VERSION_MAJOR@
和@Tutorial_VERSION_MINOR@
將會被替換爲CmakeLists.txt
中的1和0。 而後修改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;
}
複製代碼
而後執行
cmake .
make
./Tutorial
複製代碼
便可看到輸出內容: