日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

cmake使用教程(一)-起步

發布時間:2025/3/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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使用教程(一)-起步的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。