CMakeList.txt quick fix

Table of contents

I. Overview

2. Common commands and parameters

3. Detailed explanation of examples

4. Additional parameters


I. Overview

When using the CMake build system in a CMake project, you need to create a file named CMakeLists.txt to define the project's structure, configuration, and build process. CMakeLists.txt file is a text file that uses CMake syntax to describe the project's build rules and dependencies.

2. Common commands and parameters

A variety of commands and parameters can be used in the `CMakeLists.txt` file to define the structure, configuration and build process of the project. The following is an explanation of some commonly used CMake commands and parameters:

1. `cmake_minimum_required(VERSION <version>)`:

Specify the minimum required CMake version. For example, `cmake_minimum_required(VERSION 3.12)` means that the project needs to use CMake with at least version 3.12.

2. `project(<name> [VERSION <version>] [LANGUAGES <languages>])`:

Set the project's name and related options. You can use `project(<name>)` to set the name of the project, optionally `VERSION` to specify the version number of the project, and `LANGUAGES` to specify the programming language to use.

3. `add_executable(<name> [source1] [source2] ...)`:

Add an executable file. You can specify one or more source files to be compiled into an executable file.

4. `add_library(<name> [SHARED|STATIC|MODULE] [source1] [source2] ...)`:

Add a library file. You can specify one or more source files to be compiled into library files, and you can use the `SHARED`, `STATIC` or `MODULE` parameters to specify the type of library file to be generated.

5. `target_link_libraries(<target> <library1> <library2> ...)`:

Configure the link library. Link target files with library files so that the target can use the functions and symbols defined in these libraries.

6. `include_directories(<dir1> [dir2] ...)`:

Specify the directory of header files to include. You can specify one or more directories for the compiler to search for header files.

7. `link_directories(<dir1> [dir2] ...)`:

Specify the library file directory to be linked. You can specify one or more directories for the linker to search for library files.

8. `target_include_directories(<target> <INTERFACE|PUBLIC|PRIVATE> <dir1> [dir2] ...)`:

Sets the include directory for the specified target. You can use `INTERFACE`, `PUBLIC` or `PRIVATE` to control the scope of these containing directories.

9. `target_link_directories(<target> <INTERFACE|PUBLIC|PRIVATE> <dir1> [dir2] ...)`:

Sets the link directory for the specified target. You can use `INTERFACE`, `PUBLIC` or `PRIVATE` to control the scope of these linked directories.

10. `set(<variable> <value>)`:

Set the value of a variable. A cache variable can be set using `set(<variable> <value> CACHE <type> <docstring> [FORCE])`.

3. Detailed explanation of examples

cmake_minimum_required(VERSION 3.0)
project(TestDemo)

get_filename_component(SDKLibPath "../../sdk/lib/libTestSDK.so" ABSOLUTE)

include_directories(../../sdk/inc)
include_directories(../..)

aux_source_directory(../../case DEMO_SRCS)
aux_source_directory(../.. DEMO_SRCS)

set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -static-libasan")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -static-libasan")

add_compile_options("-Wno-format-zero-length")

add_executable(TestDemo ${DEMO_SRCS})

target_link_libraries(TestDemo gtest)
target_link_libraries(TestDemo pthread)
target_link_libraries(TestDemo dl)
target_link_libraries(TestDemo ${SDKLibPath})

If you don’t understand, read the explanation below and come back to read it again!​ 

cmake_minimum_required(VERSION 3.0)

This command specifies that the minimum required CMake version is 3.0.

project(TestDemo)

This command sets the name of the project to "TestDemo".

get_filename_component(SDKLibPath "../../sdk/lib/libTestSDK.so" ABSOLUTE)

This command obtains the absolute path of the file under the specified path. It stores the absolute path of “…/…/sdk/lib/libTestSDK.so” in the variable SDKLibPath .

include_directories(../../sdk/inc)
include_directories(..)

These two commands are used to add header file directories. They add the "../../sdk/inc" and ".." directories to the list of directories that the compiler uses to search for header files.

aux_source_directory(../../case DEMO_SRCS)
aux_source_directory(.. DEMO_SRCS)

These two commands are used to automatically add source files in the specified directory to variables. They add source files in the "../../case" and ".." directories to the variable DEMO_SRCS .

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fsanitize=address -fno-omit-frame-pointer -static-libasan")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -static-libasan")

These two commands are used to set the compilation options of the compiler. They add the -fsanitize=address -fno-omit-frame-pointer -static-libasan option to the C and C++ compiler options.

add_compile_options("-Wno-format-zero-length")

This command is used to add compilation options. It adds the -Wno-format-zero-length option to the compiler's options for suppressing warnings about zero-length format strings.

add_executable(TestDemo ${DEMO_SRCS})

This command is used to add an executable file TestDemo, and its source file is the source file specified in the DEMO_SRCS variable.

target_link_libraries(TestDemo gtest)
target_link_libraries(TestDemo pthread)
target_link_libraries(TestDemo dl)
target_link_libraries(TestDemo ${SDKLibPath})

These four commands are used to configure the link library. They will be gtest, pthread, dl and ${SDKLibPath} (i.e. the absolute paths obtained previously) Link to the executable file TestDemo so that the executable file can use the functions and symbols defined in these libraries.

4. Additional parameters

Continuously updating...

Guess you like

Origin blog.csdn.net/weixin_36389889/article/details/131852491