Introduction of CMake

CMake is an open-source, cross-platform family of tools designed to build, test and package software. CMake is used to control the software compilation process using simple platform and compiler independent configuration files, and generate native makefiles and workspaces that can be used in the compiler environment of your choice. The suite of CMake tools were created by Kitware in response to the need for a powerful, cross-platform build environment for open-source projects such as ITK and VTK.

Generate an executable file

CMakeList.txt

PROJECT (program name) SET (SRC_LIST 1 source file 2... ADD_EXECUTABLE(executable file name${SRC_LIST})
Copy the code
Cmake_minimum_required (VERSION 2.6) Project (Tutorial) add_executable(Tutorial tutorial.cxx)Copy the code

Add a version number:

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 Config. H.i n file

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

Use version numbers in source code

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

Generating shared libraries

instruction

Note: the instructions case-insensitive, parameter case related ADD_LIBRARY (libname [SHARED | STATIC | MODULE] [EXCLUDE_FROM_ALL] source1 source2… SourceN (SHARED library) STATIC: this library is valid on systems that use DYLD. If dyLD is not supported, it is treated as SHARED. EXCLUDE_FROM_ALL: the EXCLUDE_FROM_ALL parameter means that the library will not be built by default unless there are other component dependencies or manual builds. PROJECT instruction syntax: PROJECT (PROJECT name [C] [C++] [Java]) defines the PROJECT name and specifies the languages supported by the PROJECT. The list of supported languages is negligible. All languages are supported by default. This directive implicitly defines two cmake variables:

The variable name instructions
<projectname_BINARY_DIR> Project compilation directory
<projectname_SOURCE_DIR> Project source code directory

The Cmake system preorders two variables and . The value of the variable must be the same as the preceding two variables. Syntax of MESSAGE command: MESSAGE ([SEND_ERROR][STATUS][FATAL_ERROR] “strings”) SEND_ERROR: generated error was skipped STATUS: FATAL_ERROR: Immediately terminates the cmake process