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

Contents

  • CMake 使用说明

    • 基础 (第一步)

      • 添加版本号和配置头文件
      • 指定(使用的)c++标准
      • Build and Test
    • Adding a Library 添加库 (第二步)
    • Adding Usage Requirements for Library 添加库的使用需求 (第三步)
    • Installing and Testing 安装&测试(第四步)

      • Install Rules 安装规则
      • Testing Support 测试支持
    • Adding System Introspection 添加系统(?自省)(第五步)

      • Specify Compile Definition 指定编译定义
    • Adding a Custom Command and Generated File 添加自定义命令和生成的文件(第六步)
    • Building an Installer (第七步)
    • Adding Support for a Dashboard(控制面板?) (第八步)
    • Mixing Static and Shared 混合静态和共享 (第九步)
    • Adding Generator Expressions 添加生成器表达(第十步)
    • Adding Export Configuration 添加导出配置(第十一步)
    • Import a CMake Project 导入CMake项目(Consumer用户)
    • Packaging Debug and Release 打包调试和发布(MultiPackage)

CMakeLogo.gif
CMake 说明提供了一个大家可以一步步跟着走的教程,覆盖了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)

最基础的工程是一个编译一个可执行源代码.对于最简单的例子, 三行的CMakeLists.txt文件即可满足需求. 这是我们教程的开始.
创建一个CMakeLists.txt文件在Step1文件夹中:
自己创建...

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 
#include 
#include 
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 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.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 
#include 
#include 
#include 
#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.

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

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 
//#include 
#include 
#include 
#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@

你可能感兴趣的:(cmake,linux)