Recently, I learned the compilation knowledge of C + development under Linux and summarized this series of notes. This is the fifth one. You can read the previous notes through the following links:
1. g + + tools for compiling C + + code in Linux and common operation instructions of g + +
2. C + + command line compilation example under Linux
3. Common instructions of GDB debugger under Linux
4. C + + command line debugging practice under Linux
1. General
- Cmake (Cross Platform Make) is a cross platform compilation tool, which can describe the cross platform compilation process with simple statements
- CMake has become the primary compilation tool for most C + + open source projects
On different operating systems, we can use different compilation tools, such as Visual Studio development kit in windows, xcode development kit in mac, Makefile and g + + compilation in Linux. If we use cmake, we can solve the cross platform problem.
2. cmake basic knowledge
2.1. The basic syntax characteristics of cmake
Instruction name(Parameter 1 Parameter 2)
Parameters are enclosed in parentheses, separated by spaces or semicolons. The instructions of cmake are not case sensitive, but the parameters are strictly case sensitive, as shown in the following example
set(HELLO hello.cpp) add_executable(hello main.cpp hello.cpp) ADD_EXECUTABLE(hello main.cpp ${HELLO})
The variable of cmake instruction reads the variable in the form of ${variable}, but in IF logic judgment, you can directly enter the variable name, such as HELLO in the above example code
2.2. Important instructions and common variables of cmake
- cmake_minimum_required: Specifies the minimum version requirement for cmake
# Specify that the minimum version requirement of cmake is 2.8.3 cmake_minimum_required(VERSION 2.8.3)
- Project: define the project name and specify the language that the project can support. The syntax format is project (project domain name language), as shown in the following example
# The name of the specified project is HELLO project(HELLO)
- set: display definition variables
# Define SRC variable value as: Hello cpp main. cpp set(SRC hello.cpp main.cpp)
- include_directories: add multiple header file search paths to the project, which is equivalent to specifying the - I parameter of g + +
# Add / usr/include and/ The include path is added to the header file search path include_directories(/usr/include ./include)
- link_directories: add multiple library file search paths to the project, which is equivalent to the - L parameter of g + +
# Add / usr/lib and/ Add the Lib path to the library file search path link_directories(/usr/lib ./lib)
- add_library: generate library files
# Generate hello. Through variable SRC So SHARED library, and the second parameter is filled in SHARED or STATIC or MOUDLE add_library(hello SHARED ${SRC})
- add_compile_options: add compilation parameters
# Add compilation parameters - Wall -std=c++11 and - o2 add_compile_options(-Wall -std=c++11 -o2)
- add_excutable: generate executable file
# Compile main CPP generate executable file main add_excutable(main main.cpp)
- target_link_libraries: add the required shared libraries for the target executable
# Link the hello dynamic library file to the executable file main target_link_libraries(main hello)
- add_subdirectory: add a subdirectory for storing source files to the current project, and specify the storage location of intermediate binary and target binary
# Add the src subdirectory. There needs to be a cmakelists in src txt add_subdirectory(src)
- aux_source_directory: find all source code files in a directory and store the list in a variable. This instruction is temporarily used to automatically build the source file list.
# Defines the SRC variable whose value is all source code files in the current directory aux_source_directory(. SRC) # Compile the source code file under SRC variable to generate mian executable file add_excutable(main ${SRC)
2.3. Cmake common variables
- CMAKE_ Flags: GCC compilation option
- CMAKE_ CXX_ Flags: G + + compilation option
# At cmake_ CXX_ Add - std=c++11 after flags compilation option set(CMAKE_CXX_FLAGS "{CMAKE_CXX_FLAGS} -std=c++11")
- CMAKE_BUILD_TYPE: compile type (Debug, Release)
# Set the compilation type to debug. You need to select debug during debugging set(CMAKE_BUILD_TYPE Debug) # Set the compilation type to release, and select release when publishing set(CMAKE_BUILD_TYPE Release)
- CMAKE_BINARY_DIR,PROJECT_BINARY_DIR,<projectname>_BINARY_DIR
- The three variables refer to the same content
- If it is in binary build, it refers to the top-level directory of the project
- For out of binary compilation, it refers to the directory where the project compilation takes place
- PROJECT_BINARY_DIR is slightly different from other instructions, but it can be understood that they are consistent
- CMAKE_SOURCE_DIR,PROJECT_SOURCE_DIR,<projectname>_SOURCE_DIR
- The contents referred to by these three variables are consistent. No matter what compilation method is adopted, they are all engineering layer directories
- If it is in source build, follow CMAKE_BINARY_DIR variable consistent
- PROJECT_SOURCE_DIR is slightly different from other instructions, but it can be understood that they are consistent
- CMAKE_C_COMPILE: Specifies the C compiler
- CMAKE_CXX_COMPILE: Specifies the C + + compiler
- EXECUTABLE_OUT_PATH: the storage path of the executable output
- LIBRARY_OUTOUT_PATH: storage path of library file output