cmake introductory notes

Learn camke to compile for large projects

Preliminary knowledge:

1. Linux basic commands: such as ls, cd, vim, apt or yum (different download programs are used in different Linux systems), cat, etc.

2. Linux download cmake (usually comes with it)

3. The process of c/c++ forming an executable file

1. A single source file (like cmake)

Method one:

gcc -o main main.c

Law 2:

g++ -o main main.cpp

Method three:

step1: Write a .cpp file and put it in an empty folder

step2: Write CMakeLists.txt under this folder 

#CMakeLists.txt
PROJECT(HELLO)
SET(SRC_LIST main.cpp)
MESSAGE(STATUS "This is BINARY dir" ${HELLO_SOURCE_DIR})
MESSAGE(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})

step3: Use cmake to generate makefille files

cmake . 

cmake at current location (generates makeflie file)

Generated file:

 step4 Compile makeflie at the current file location

make

Generate executable file main

step5 Execute the executable file

./main

-----------------------------------------------------

Introduction to cmake syntax:

#CMakeLists.txt
PROJECT(HELLO)
SET(SRC_LIST main.cpp)
MESSAGE(STATUS "This is BINARY dir" ${HELLO_SOURCE_DIR})
MESSAGE(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
ADD_EXECUTABLE(hello ${SRC_LIST})

The PROJECT keyword is used to specify the name of the project and the supported languages, all languages ​​are supported by default .

         PROJECT (HELLO) specifies the project name as HELLO

         PROJECT (HELLO CXX) project name is HELLO and supports c++

         PROJECT (HELLO C CXX) project name is HELLO and supports c and c++

          This specification implicitly defines two cmake variable tips: <projectname> is the project name

         <projectname>_BINARY_DIR in this case HELLO_BINARY_DIR

        <projectname>_SOURCCE_DIR in this case HELLO_SOURCE_DIR

       The MESSAGE keyword can be used directly. These two variables currently point to the current working directory, and we will talk about external compilation later. 

  If the project name is changed, these two variable names will also change

Two predefined variables PROJECT_BINARY_DIR and PROJECT_SOURCCE_DIR These two variables are consistent with HELLO_SOURCE_DIR HELLO_BINARY_DIR

SET keyword

        used to display the specified variable

        SET(SRC-LIST main.cpp ) The SRC_LIST variable contains main.cpp

        Can also be SET(SRC_LIST main.cpp t1.cpp t2.cpp)

MESSAGE keywords

Output custom information to the terminal

        Contains three

         SEND_ERROR generated an error and the build process was skipped

        SATUS outputs prefix-unique information

        FATAL_ERROR terminates all cmake processes immediately

For example, this keyword in this example will output

 

 

ADD_EXECUTABLE keyword

        generate executable

        ADD_EXECUTABLE(hello ${SRC_LIST}) Generate an executable file named hello The source file reads the contents of the variable SRC_LIST and can also be directly written as ADD_EXECUTABLE(hello main.cpp)

The above example can be simplified to:

PROJECT(HELLO)

ADD_EXECUTABLE(hello main.cpp)

Note that the project name HELLO has nothing to do with the executable file name hello

Basic Principles of Grammar

Variables use ${} to take values, but in the IF statement, the variable name is used directly

Instructions (parameter 1, parameter 2...) parameters use brackets to expand the parameters and separate them with spaces and semicolons. Take ADD_EXECUTABLE(hello main.cpp) as an example

Instructions are case-insensitive and variable parameters are case-sensitive

Recommend all caps 

grammar attention 

SET(SRC_LIST main.cpp) can be written as SET(SRC_LIST "main.cpp") If the file name has spaces, "" must be added

ADD_EXECUTABLE(hello main) suffix can not be written, it will automatically search for .c .cpp, try not to write like this 

Built Inside and Built Outside

The above-mentioned temporary files generated for internal components are inconvenient to clean up,

The external build is to put the cmake generated file into the build folder for easy recompilation

2.

Guess you like

Origin blog.csdn.net/m0_73580268/article/details/130088325