Link to CMake tutorial

Introduce a,

The CMake tutorial provides a step-by-step guide that covers solutions to common problems encountered during the CMake build process. It is helpful to see how the various topics in the sample project work together. The tutorial documentation and source code for the sample can be found in the Help/ Guide/Tutorial directory of the CMake source tree. Each step has its own subdirectory that contains code that can be used as a starting point. The tutorial examples are incremental, so each step provides a complete solution to the previous one.

Step 1

Most projects build executables from source code. We’ll start the tutorial with a simple project that requires a cMakelists.txt file containing only three lines.

2.1 tutorial. CXX

// A simple program that computes the square root of a number
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <string>
 
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;
}
Copy the code

2.2 build

Create a cmakelists. TXT file in the Step1 directory as follows:

cmake_minimum_required(VERSION 3.10)
 
# set the project name# Set the project nameproject(Tutorial)
 
# add the executable# Add executable fileadd_executable(Tutorial tutorial.cxx)
Copy the code

Note that in this example, we use lowercase commands in the cMakelists.txt file. CMake supports uppercase, lowercase, and mixed-case commands. The tutorial. CXX directory provides the source code, and Step1 can be used to calculate the square root of a number.

cmake_minimum_required(VERSION […] [FATAL_ERROR]) sets the minimum cmake version required for the project.

project( […] ) project( [VERSION [.[.[.]]]] [DESCRIPTION ] [HOMEPAGE_URL ] [LANGUAGES …] ) and store it in the variable PROJECT_NAME. Calling cMakelists.txt from the top level also stores the project name in the variable CMAKE_PROJECT_NAME