CMake2:版本号配置与头文件生成
生活随笔
收集整理的這篇文章主要介紹了
CMake2:版本号配置与头文件生成
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.基本測試?
最基本的功能就是利用源代碼文件生成一個可執行程序。 CMakeLists.txt: cmake_minimum_required ( VERSION 3.5) project (Tutorial) add_executable(Tutorial tutorial.c)Tutorial.c: // 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[]) {double inputValue = 4;double outputValue = sqrt(inputValue);fprintf(stdout,"The square root of %g is %g\n",inputValue, outputValue);return 0; }利用CMake組建,VisualStudio編譯就可以生成可執行文件Tutorial.exe。 這里會出現一個問題就是Tutorial.exe點擊后出現閃退,采用一下方法可以解決。- exe目錄下新建一個文本文檔:新建文檔.txt;
- 打開文檔,鍵入內容:Tutorial.exe (換行) pause ?注意XX.exe,XX是你的實際應用程序名;
- 保存文檔,名字+后綴改為:Tutorial.bat;
- 點擊Tutorial.bat文件即可解決閃退問題。
2.增加版本號并配置頭文件
首先討論的是給我們的項目/可執行程序添加一個版本號。當然我們可以在源代碼中設置,但是在CMake中設置版本號會更加靈活。 首先我們要修改CMakeLists.txt如下: cmake_minimum_required (VERSION 3.5) 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.c)因為配置文件將要被寫入到二進制文件結構中,所以我們必須要把該文件添加到相應的路徑列表中。首先,我們在源路徑下創建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生成相應的TutorialConfig.h文件!! 當CMake配置頭文件的時候,?@Tutorial_VERSION_MAJOR@ 和 @Tutorial_VERSION_MINOR@ 的值就會被 CMakeLists.txt 文件中得值所取代。 下面修改原文件Tutorial.c,使其包括配置的頭文件并使用版本號,源代碼修改如下: // 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[]) {double inputValue = 4;double outputValue = sqrt(inputValue);fprintf(stdout,"The square root of %g is %g\n",inputValue, outputValue);fprintf(stdout,"%s Version %d.%d\n",argv[0],Tutorial_VERSION_MAJOR,Tutorial_VERSION_MINOR);return 0; }輸出結果:總結
以上是生活随笔為你收集整理的CMake2:版本号配置与头文件生成的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 今天开始学习ASP
- 下一篇: CMake3:添加一个库