cmake官方入门说明Step1+一通执行过程

in 网站建设
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9

Contents

CMakeLogo.gif
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/tutorialdirectory 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.

A Basic Starting Point (Step 1)

The most basic project is an executable built from source code files. For simple projects, a three lineCMakeLists.txtfile is all that is required. This will be the starting point for our tutorial.
Create aCMakeLists.txtfile in theStep1directory that looks like:
自己创建...

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.txtfile.
Upper, lower, and mixed case commands are supported by CMake.
大小写不敏感
The source code fortutorial.cxxis provided in theStep1directory and can be used to compute the square root of a number.

// 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;
}

Adding a Version Number and Configured

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.txtprovides more flexibility.

First, modify theCMakeLists.txtfile to set the version number.
为了增加版本号,我们可以更改 CMakeLists 文件

cmake_minimum_required(VERSION  3.10)
#设置工程名和版本号
project(Tutorial  VERSION  1.0)

Then, configure a header file topass 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.txtfile:

target_include_directories(Tutorial  PUBLIC
  "${PROJECT_BINARY_DIR}"
  )

Using your favorite editor, createTutorialConfig.h.inin 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.cxxto include the configured header file,TutorialConfig.h.

Finally, let’s print out the version number by updatingtutorial.cxxas 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;
}

Specify the C++ Standard

Next let’s add some C++11 features to our project by replacingatofwithstd::stodintutorial.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_STANDARDvariable.
For this tutorial, set theCMAKE_CXX_STANDARDvariable in theCMakeLists.txtfile to 11 andCMAKE_CXX_STANDARD_REQUIREDto 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)

Build and Test

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 .

1.png

很好,不愧是我,没有错误是不可能的(shift)
不想升级的我修改了版本号

cmake_minimum_required(VERSION 3.5)

再来
2.png
成了,在往下看
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

3.png
很好,又又不对
回头再看一遍,首先我生成文件的地方不太对,不过这不是很重要
然后我没有build...
Screenshot from 2019-12-21 16-43-01.png
然后吧我直接按教程那样的确是不行的,我还暂时不知道为什么,但是加上./就可以了

./Tutorial 4294967296
./Tutorial 10
./Tutorial

哦,最后再看一下所有文件最后的状态吧
4.png
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@
关注公众号【好便宜】( ID:haopianyi222 ),领红包啦~
阿里云,国内最大的云服务商,注册就送数千元优惠券:https://t.cn/AiQe5A0g
腾讯云,良心云,价格优惠: https://t.cn/AieHwwKl
搬瓦工,CN2 GIA 优质线路,搭梯子、海外建站推荐: https://t.cn/AieHwfX9
扫一扫关注公众号添加购物返利助手,领红包
Comments are closed.

推荐使用阿里云服务器

超多优惠券

服务器最低一折,一年不到100!

朕已阅去看看