[CMake entry and advanced (4)] CMakeLists.txt grammar rules basics and some commonly used instructions-continued (with code)

        Due to space issues, this article continues to introduce the basics of CMakeLists.txt grammar rules and common instructions.

  • aux_source_directory

        The aux_source_directory command will find all source files in the directory, and its command definition is as follows:

aux_source_directory(<dir> <variable>)

        Find all source files from the specified directory, and store the scanned source file path information in variables, for example, the directory structure is as follows:

├── build
├── CMakeLists.txt
└── src
  ├── 1.c
  ├── 2.c
  ├── 2.cpp
  └── main.c

        The content of CMakeCache.txt is as follows:

# 顶层 CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project("HELLO")

# 查找 src 目录下的所有源文件
aux_source_directory(src SRC_LIST)
message("${SRC_LIST}") # 打印 SRC_LIST 变量

        Go to the build directory, execute the cmake .. command, and the printed information is as follows: 

         It can be seen that aux_source_directory will add each scanned source file to the SRC_LIST variable to form a list of strings separated by a semicolon ";".

        Similarly, aux_source_directory can use either a relative path or an absolute path, and the relative path is relative to the current source code path.

  •  get_target_property 和 set_target_properties

        Attributes for getting/setting targets respectively.

  • include_directories include_directories

        The command is used to set the search path of the header file, which is equivalent to the -I option of gcc, and its definition is as follows:

include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])

        By default, the specified directory will be added to the end of the header file search list (it can be considered that each CMakeLists.txt source code has its own header file search list), you can change its default behavior by setting the CMAKE_INCLUDE_DIRECTORIES_BEFORE variable to ON, and add the directory to the front of the list. You can also use the AFTER or BEFORE option each time you call the include_directories command to specify whether to add to the front or back of the list. If the SYSTEM option is used, the specified directory will be regarded as the system search directory. Either an absolute path can be used to specify the header file search directory, or a relative path can be used, and the relative path is interpreted as the relative path of the current source code path.

        For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
├── include
│ └── hello.h
└── main.c

        The source file main.c uses the header file hello.h under the include directory, and the content of CMakeLists.txt is as follows:

# 顶层 CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project("HELLO")

include_directories(include)
add_executable(hello main.c)

        Use the include_directories command to add the include folder in the current directory to the header file search list, enter the build directory, and execute cmake and make to build and compile. There is no problem in the compilation process, and there will be no error prompting that the header file cannot be found; but If you remove the command include_directories (include), the compilation will definitely report an error, you can try it yourself!

        By default, the include directory is added to the end of the header file search list, and can be explicitly specified to be added after or before the list through the AFTER or BEFORE options:

# 添加到列表后面
include_directories(AFTER include)

# 添加到列表前面
include_directories(BEFORE include)

        When the add_subdirectory command is called to load the sub-source code, the list of directories contained in the include_directories command will be passed down to the sub-source code (the sub-source code is inherited from the parent source code). Let's test it. For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
├── include
│ └── hello.h
└── src
  ├── CMakeLists.txt
  └── main.c

        The hello.h header file is used in the main.c source file under the src directory, and the content of the top-level CMakeLists.txt is as follows:

# 顶层 CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project("HELLO")

include_directories(include)
add_subdirectory(src)

        The top-level CMakeLists.txt source code calls include_directories to add the include directory to the header file search list of the current source code, and then calls the add_subdirectory command to load and execute the sub-source code; the content of CMakeLists.txt under the src directory is as follows:

# src 目录 CMakeLists.txt
add_executable(hello main.c)

        Go to the build directory, build and compile, and the whole compilation process is no problem.

  • link_directories 和 link_libraries link_directories

        The command is used to set the search path of library files, which is equivalent to the -L option of the gcc compiler; the link_libraries command is used to set the library files to be linked, which is equivalent to the -l option of the gcc compiler; the command definition is as follows:

link_directories(directory1 directory2 ...)
link_libraries([item1 [item2 [...]]]
                 [[debug|optimized|general] <item>] ...)

        link_directories will add the specified directory to the library file search list (it can be considered that each CMakeLists.txt source code has its own library file search list); similarly, the link_libraries command will add the specified library file to the link library list. The link_directories command can use absolute or relative paths to specify directories, and relative paths are interpreted as relative to the current source code path.

        For example, the project directory structure is as follows:

├── build
├── CMakeLists.txt
├── include
│ └── hello.h
├── lib
│ └── libhello.so
└── main.c

        There is a dynamic library file libhello.so in the lib directory, libhello.so needs to be linked when compiling and linking the main.c source file; the content of the CMakeLists.txt file is as follows:

# 顶层 CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project("HELLO")

include_directories(include)
link_directories(lib)
link_libraries(hello)
add_executable(main main.c)

        The library file name can be abbreviated or the full name of the library file name, for example:

# 简写
link_libraries(hello)

# 全称
link_libraries(libhello.so)

        The link_libraries command can also specify the full path of the library file (absolute path/beginning). If it is not at the beginning of /, link_libraries will think that the caller passed in the name of the library file instead of the full path of the library file. For example, the above CMakeLists.txt can be modified as This way:

# 顶层 CMakeLists.txt
cmake_minimum_required("VERSION" "3.5")
project("HELLO")

include_directories(include)
link_libraries(${PROJECT_SOURCE_DIR}/lib/libhello.so)
add_executable(main main.c)

        Same as the include_directories command, when the add_subdirectory command is called to load the subsource code, the directory list contained in the link_directories command and the link library list contained in the link_libraries command will be passed down to the subsource code (the subsource code is inherited from the parent source code). Here is no longer a demonstration, you can test it yourself.

  • list

        The list command is a command about list operations, such as getting the length of the list, returning the element specified by the index value from the list, appending the element to the list, and so on. The commands are defined as follows:

list(LENGTH <list> <output variable>)
list(GET <list> <element index> [<element index> ...]
      <output variable>)
list(APPEND <list> [<element> ...])
list(FIND <list> <value> <output variable>)
list(INSERT <list> <element_index> <element> [<element> ...])
list(REMOVE_ITEM <list> <value> [<value> ...])
list(REMOVE_AT <list> <index> [<index> ...])
list(REMOVE_DUPLICATES <list>)
list(REVERSE <list>)
list(SORT <list>)

           The concept of a list has not been introduced to you yet. A list is actually an array of strings (or a list of strings, or an array of strings), which I will explain to you later.

        The LENGTH option is used to return the length of the list;

        The GET option returns the element specified by the index value from the list;

        The APPEND option appends elements to the list;

        The FIND option will return the index value of the specified element in the list, or -1 if not found.

        The INSERT option will insert an element into the list at the specified position.

        The REMOVE_AT and REMOVE_ITEM options will remove elements from the list, the difference is that REMOVE_ITEM will remove the given element, while REMOVE_AT will remove the element at the given index value.

        The REMOVE_DUPLICATES option will remove duplicate elements in the list.

        The REVERSE option reverses the contents of the list in-place.

        The SORT option sorts the list alphabetically.

  • message

        The message command is used to print and output information, similar to the Linux echo command, the command definition is as follows:

message([] "message to display" ...)

        The optional mode keyword is used to determine the type of message, as follows:

mode illustrate
none (none) important information, general information
STATUS Additional information
WARNING CMake warning, continue processing
AUTHOR_WARNING CMake warning (development), continue processing
SEND_ERROR CMake error, continue processing, but skip build
FATAL_ERROR CMake error, stop processing and build
DEPRECATION CMake deprecates errors or warnings if the variables CMAKE_ERROR_DEPRECATED or CMAKE_WARN_DEPRECATED respectively are enabled, otherwise no message.

        So you can use this command as an output print statement in the CMakeLists.txt source code, for example:

# 打印"Hello World"
message("Hello World!")

To be continued...

Guess you like

Origin blog.csdn.net/cj_lsk/article/details/131062552