Cmake (cmake)

Cmake Use tutorial (1) – start

Cmake Tutorial 2 – Adding libraries

Cmake use tutorial (3) – installation, testing, system self – check

Cmake Use tutorial (4) – file generator

Cmake Use tutorial (5) -cpack generate installation package

Cmake Use tutorial (6) – lousy syntax

Cmake Use Tutorial (7) – Processes and loops

Cmake: Macro and Function

This series of articles was translated from the official Cmake tutorial: CMake Tutorial.

Example program address: github.com/rangaofei/t…

It will not stop at the official tutorial. I as an Android developer, is really no Linux C program development experience, hope big guys forgive. The tutorial is done on MacOS, and I’ve tested most Linux as well, so IF there are special instructions, I’ll note them. This tutorial is based on cmake-3.10.2 and assumes that you have cmake installed.

The basic grammar

A basic cMakelists.txt file should contain at least three lines:

Cmake_minimum_required (VERSION 2.6) project (Tutorial) add_executable(Tutorial tutori.cxx)Copy the code

Note: Cmake syntax supports size, lowercase and case-mixing The cmake syntax we used in the above code is lowercase.

cmake_minimum_required
CMAKE_MINIMUM_REQUIRED
cmake_MINUMUM_required
Copy the code

All three are the same. Note that only system instructions are case insensitive, but variables and strings are case sensitive.

Create a tutorial. CXX file that calculates the square root of a number as follows:

// 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;
}
Copy the code

This completes a simple cmake program.

A builder

Compile this code with cmake, go to the command line and execute the internal build command (external build later) :

cmake .
Copy the code

So this is the output of a bunch of logs

-- The C compiler identification is AppleClang 9.0.0.9000039 -- The CXX Compiler identification is AppleClang 9.0.0.9000039 9.0.0.9000039 - Checkfor working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/saka/Desktop/Tutorial/Step1
Copy the code

Three files CMakeCache. TXT, Makefile, cmake_install.cmake and a folder CmakeFiles are generated and executed

make 
Copy the code

Executable Tutorial can be generated. On Ubuntu or centos there may be a message indicating that the math.h file cannot be found, in which case we need to add it last in the cmakelists.txt file

target_link_libraries(Tutorial apue.a)
Copy the code

Then recompile. You need to delete the extra files you just generated.

Add version number

Here’s how to add a version number to your program and a header file with a version number to use.

Set (KEY VALUE) takes two arguments, which are used to declare variables. ${KEY} ${KEY} ${KEY} ${KEY}

Cmake_minimum_required (VERSION 2.6) Project (Tutorial)# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
 
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  )
 
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
 
# add the executable
add_executable(Tutorial tutorial.cxx)
Copy the code

The configuration files will be written to the executable directory, so our project must include this folder to use the configuration headers. We need to create a new tutorialconfig.h.id in the project directory as follows:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
Copy the code

@tutorial_version_major @ and @tutorial_version_minor @ in the above code will be replaced with 1 and 0 in cmakelists.txt. Then modify the tutorial. CXX file as follows to output version information without additional parameters:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
 
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"%s Version %d.%d\n",
            argv[0],
            Tutorial_VERSION_MAJOR,
            Tutorial_VERSION_MINOR);
    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;
}
Copy the code

Then perform

cmake .
make
./Tutorial
Copy the code

You can see the output: