cmake官方入門說明Step1+一通執行過程

Contentsios

  • CMake 使用說明c++

    • 基礎 (第一步)ide

      • 添加版本號和配置頭文件
      • 指定(使用的)c++標準
      • Build and Test
    • Adding a Library 添加庫 (第二步)
    • Adding Usage Requirements for Library 添加庫的使用需求 (第三步)
    • Installing and Testing 安裝&測試(第四步)測試

      • Install Rules 安裝規則
      • Testing Support 測試支持
    • Adding System Introspection 添加系統(?自省)(第五步)flex

      • 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
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.ui

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:
本身建立...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.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.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;
}

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 文件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.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@
相關文章
相關標籤/搜索