cmake使用教程(一)-起步
【cmake系列使用教程】
cmake使用教程(一)-起步
cmake使用教程(二)-添加庫
cmake使用教程(三)-安裝、測試、系統自檢
cmake使用教程(四)-文件生成器
cmake使用教程(五)-cpack生成安裝包
cmake使用教程(六)-蛋疼的語法
cmake使用教程(七)-流程和循環
cmake使用教程(八)-macro和function
這個系列的文章翻譯自官方cmake教程:cmake tutorial。
示例程序地址: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 復制代碼即可看到輸出內容:
總結
以上是生活随笔為你收集整理的cmake使用教程(一)-起步的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: randomaccessfile在移动设
- 下一篇: 18.6 负载均衡集群介绍 18.7 L