ubuntu下cmake學習
阿新 • • 發佈:2017-11-18
efi ima 分享 path main 增加 tree define sub
這個系列的博客是是從cmake 官方學來的,相當於是他的中文版吧,原版請看https://cmake.org/cmake-tutorial/
官方教程有7步,我打算只講(fanyi)前2步,後面的基本都類似,看官方教程即可。
Step 1:新建一個可執行程序
首先確保你已經安裝了cmake 和 g++,如果沒有安裝,就:
sudo apt-get install cmake g++
然後準備一個工作空間,並準備一些素材:
mkdir learn_cmake_again cd learn_cmake_again touch CMakeLists.txt touch tutoria.cpp
然後給文件加內容:
#CMakeLists.txt文件內容 #版本號 cmake_minimum_required (VERSION 2.8) #項目名 project (Tutorial) #生成可執行程序 語法:add_executable(可執行程序名 要編譯的cpp) add_executable(Tutorial tutorial.cpp)
//cpp文件中的內容 // 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 . make
./Tutorial 16
結果很明顯是對的,哈哈,接下來一個任務就要往程序裏面增加一點變量
CMakeLists.txt中的內容為:
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 #.h.in文件是可以自動編譯的文件,通過下面這個命令可以得到.h文件
# 關於PROJECT_BINARY_DIR和PROJECT_SOURCE_DIR的區別可以查看:http://blog.csdn.net/sukhoi27smk/article/details/46388711,本教程中就不做區分了 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.cpp)
//cpp文件裏的內容 // 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; }
//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 . make
就會看到自動生成了TutorialConfig.h文件,而且此時cpp裏還可以打印出CMakeLists.txt裏定義的變量,縱觀來看,.in文件相當於是一個橋梁,架起了cmakelists中的變量到cpp的傳遞。
平時看項目時的CMakeLists.txt可沒那麽簡單,經常會包含新建庫,然後鏈接庫的步驟。
Step 2:添加一個庫
此時我們需要改一下文件結構了,如圖
子目錄
以下是各個文件的內容:
#外部的CMakeLists.txt cmake_minimum_required (VERSION 2.8) 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" ) # should we use our own math functions?
# 相當於是個flag option (USE_MYMATH "Use tutorial provided math implementation" ON) # add the binary tree to the search path for include files # so that we will find TutorialConfig.h # add the MathFunctions library? # if (USE_MYMATH) include_directories ("${PROJECT_SOURCE_DIR}/MathFunctions") add_subdirectory (MathFunctions) #將MathFunctions中的子目錄也包含進去 set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions) # set語法: set(<變量> <值> <值>),把值賦給變量,多個值的話,就把多個值拼接賦給變量 endif (USE_MYMATH) # add the executable add_executable (Tutorial tutorial.cpp) target_link_libraries (Tutorial ${EXTRA_LIBS})
//tutorial.cpp的內容 // A simple program that computes the square root of a number #include <stdio.h> #include <stdlib.h> #include <math.h> #include "TutorialConfig.h" #ifdef USE_MYMATH #include "MathFunctions.h" #endif 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]);
//根據外部的情況,選擇用哪一部分程序,在實際項目中經常用到,比如有多個版本的opencv時,就可以由多個選擇,提高兼容性 #ifdef USE_MYMATH double outputValue = mysqrt(inputValue); #else double outputValue = sqrt(inputValue); #endif fprintf(stdout,"The square root of %g is %g\n", inputValue, outputValue); return 0; }
//TutorialConfig.h.in文件內容,.h文件不用寫會自動生成 // the configured options and settings for Tutorial
//它的作用主要是用於生成.h文件,所以具體看.h文件體會其作用即可 #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@ #cmakedefine USE_MYMATH
#子文件夾中的CMakeLists.txt add_library(MathFunctions mysqrt.cpp)
//MathFunctions.h內容 #include<iostream> #include<math.h> double mysqrt(double a) ;
//mysqrt.cpp內容 #include<iostream> #include<math.h> #include<MathFunctions.h> using namespace std; double mysqrt(double a) { cout<<"its my sqrt"<<endl; return sqrt(a); }
之後cmake . 和make,就可以了
ubuntu下cmake學習