CMake Practice Study Notes (a): use cmake to compile a simple program

The completion of a simple program to build

Create a new folder, contains the following two files

  • main.c
#include <stdio.h>
int main()
{
   printf("Welcome to my Blog!\n");
   return 0;
}
  • CMakeLists.txt
PROJECT(HELLO)
SET(SRC_LIST main.c)
MESSAGE(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir" ${HELLO_SOURCE_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})

Start building:

mkdir build
cd build
cmake .. #开始构建
make  #工程的实际构建
./hello #执行生成的可执行文件

Here Insert Picture Description
build the next generation of file:
Here Insert Picture Description

CMkeLists.txt profiling meaning

  • PROJECT(HELLO)
     Format: the PROJECT (ProjectName [CXX] [C] [the Java])
     1. to define the name of the project; herein can be written SET(SRC_LIST “main.c”)
     2. cmake implicitly defines two variables:
    <projectname>_SOURCE_DIR (指代工程路径)
    <projectname>_BINARR_DIR (指代编译路径)
     the front of the project name is the user self-defined; ( here the two variables directly MESSAGE printed out)
  • SET(SRC_LIST main.c)
     Format: the SET (on VAR [of VALUE] [docstring the CACHE the TYPE [the FORCE])
     where mainly used to explicitly define variables, if there are a plurality of source files can be defined sequentially SET (main.c mian2.c mian3.c)
  • MESSAGE
     Format: the MESSAGE ([SEND_ERROR | the STATUS | fatal_error] "Message to the display" ...)
     for outputting to the terminal user-defined information, comprising three types:
    SEND_ERROR: error generating process is skipped.
    SATUS: Output prefix - the
    FATALERROR: immediate termination of all process cmake
  • ADD_EXECUTABLE(hello ${SRC_LIST})
     Defines the project will generate an executable file called hello, the relevant source file is a list of source files SRC_LIST defined, CMakeLists.txt here can be changed to shorthand:
PROJECT(HELLO)
ADDEXECUTABLE(hello main.c)

Reference and notes pdf download

Published 82 original articles · won praise 124 · views 110 000 +

Guess you like

Origin blog.csdn.net/qq_44717317/article/details/104613695