Contentsios
CMake 使用說明c++
基礎 (第一步)ide
Installing and Testing 安裝&測試(第四步)測試
Adding System Introspection 添加系統(?自省)(第五步)flex
The CMake tutorial provides a step-by-step guide that covers common build system issues that CMake helps address. Seeing how various topics all work together in an example project can be very helpful. The tutorial documentation and source code for examples can be found in theHelp/guide/tutorial
directory of the CMake source code tree. Each step has its own subdirectory(子目錄)containing code that may be used as a starting point. The tutorial examples are progressive so that each step provides the complete solution for the previous step.ui
The most basic project is an executable built from source code files. For simple projects, a three lineCMakeLists.txt
file is all that is required. This will be the starting point for our tutorial.
Create aCMakeLists.txt
file in theStep1
directory that looks like:
本身建立...this
cmake_minimum_required(VERSION 3.10) # set the project name project(Tutorial) # add the executable add_executable(Tutorial tutorial.cxx)
Note that this example uses lower case commands in theCMakeLists.txt
file.
Upper, lower, and mixed case commands are supported by CMake. 大小寫不敏感
The source code fortutorial.cxx
is provided in theStep1
directory and can be used to compute the square root of a number.spa
// 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; }
The first feature we will add is to provide our executable and project with a version number. 確保咱們執行的東西要有版本號
While we could do this exclusively in the source code, usingCMakeLists.txt
provides more flexibility.調試
First, modify theCMakeLists.txt
file to set the version number.
爲了增長版本號,咱們能夠更改 CMakeLists 文件code
cmake_minimum_required(VERSION 3.10) #設置工程名和版本號 project(Tutorial VERSION 1.0)
Then, configure a header file to pass the version number to the source code:
配置一個頭文件,把版本號傳遞給源代碼
configure_file(TutorialConfig.h.in TutorialConfig.h)
Since the configured file will be written into the binary tree, we must add that directory to the list of paths to search for include files. Add the following lines to the end of theCMakeLists.txt
file:
target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" )
Using your favorite editor, createTutorialConfig.h.in
in the source directory with the following contents:
在源碼目錄中建立 TutorialConfig.h.in
文件
//the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
When CMake configures this header file the values for@Tutorial_VERSION_MAJOR@
and@Tutorial_VERSION_MINOR@
will be replaced.
Next modifytutorial.cxx
to include the configured header file,TutorialConfig.h
.
Finally, let’s print out the version number by updatingtutorial.cxx
as follows:
// A simple program that computes the square root of a number #include <cmath> #include <cstdlib> #include <iostream> #include <string> #include "TutorialConfig.h.in" int main(int argc, char* argv[]) { if (argc < 2) { std::cout << "Usage: " << argv[0] << " number" << std::endl; return 1; } // convert input to double const double inputValue = atof(argv[1]); // calculate square root const double outputValue = sqrt(inputValue); std::cout << "The square root of " << inputValue << " is " << outputValue << std::endl; return 0; }
Next let’s add some C++11 features to our project by replacingatof
withstd::stod
intutorial.cxx
. At the same time
remove#include<cstdlib>
.
const double inputValue = std::stod(argv[1]);
We will need to explicitly state in the CMake code that it should use the correct flags.
The easiest way to enable support for a specific C++ standard in CMake is by using theCMAKE_CXX_STANDARD
variable.
For this tutorial, set theCMAKE_CXX_STANDARD
variable in theCMakeLists.txt
file to 11 andCMAKE_CXX_STANDARD_REQUIRED
to True:
cmake_minimum_required(VERSION 3.10) # set the project name and version project(Tutorial VERSION 1.0) # specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True)
Run cmake or cmake-gui to configure the project and then build it with your chosen build tool.
For example, from the command line we could navigate to the Help/guide/tutorial
directory of the CMake source code tree and run the following commands:
mkdir Step1_build cd Step1_build cmake ../Step1 cmake --build .
很好,不愧是我,沒有錯誤是不可能的(shift)
不想升級的我修改了版本號
cmake_minimum_required(VERSION 3.5)
再來
成了,在往下看
Navigate to the directory where Tutorial was built (likely the make directory or a Debug or Release build configuration subdirectory) and run these commands:
Tutorial 4294967296 Tutorial 10 Tutorial
很好,又又不對
回頭再看一遍,首先我生成文件的地方不太對,不過這不是很重要
而後我沒有build...
而後吧我直接按教程那樣的確是不行的,我還暫時不知道爲何,可是加上./
就能夠了
./Tutorial 4294967296 ./Tutorial 10 ./Tutorial
哦,最後再看一下全部文件最後的狀態吧CMakeLists.txt
cmake_minimum_required(VERSION 3.5) # set the project name project(Tutorial VERSION 1.0) # specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True) configure_file(TutorialConfig.h.in TutorialConfig.h) # add the executable add_executable(Tutorial tutorial.cxx) target_include_directories(Tutorial PUBLIC "${PROJECT_BINARY_DIR}" )
tutorial.cxx
// A simple program that computes the square root of a number #include <cmath> //#include <cstdlib> #include <iostream> #include <string> #include "TutorialConfig.h.in" int main(int argc, char* argv[]) { if (argc < 2) { std::cout << "Usage: " << argv[0] << " number" << std::endl; return 1; } // convert input to double const double inputValue = std::stod(argv[1]); // calculate square root const double outputValue = sqrt(inputValue); std::cout << "The square root of " << inputValue << " is " << outputValue << std::endl; 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@