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@