Initial introduction to CMake

CMake

  • When there is only a single source file, you can compile it with the gcc command.
  • When there are multiple source files, you can use the make tool.
    • Make itself does not have the function of compiling and linking, but compiles and links by calling the commands in the makefile file.
  • The makefile is not suitable for projects with relatively large workloads, and it needs to be rewritten if it is cross-platform. You can use cmake to build the makefile
    • cmake generates makefile based on CMakeLists.txt

“gcc” --> make(makefile) -->cmake(CMakeLists.txt)

cmake build process

cmake . # 会在当前目录下找CMakeLists.txt
make # 根据cmake生成的makefile进行编译构建

Study link

CMake syntax introduction

The instructions are not case-sensitive, but the contents inside are case-sensitive.

project command

Can be used to specify the name of the project and supported languages. All languages ​​are supported by default.

project(HELLO)  # 指定工程名为HELLO,支持所有语言

SET command

Equivalent to defining variables

set(SRC_LIST main.cpp a.cpp)

MESSAGE command

Output user-defined information to the terminal

message(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
  • FATAL_ERROR: An error occurred in cmake, and it stopped processing and generating. This message has the effect of exit.
  • SEND_ERROR: An error occurred, processing continues but will not be generated.
  • WARNING: Issue a warning and continue processing.
  • AUTHOR_WARNING: CMake Warning (dev), continue processing.
  • DEPRECATION: CMake Deprecation Error or Warning if the CMAKE_ERROR_DEPRECATED or CMAKE_WARN_DEPRECATED variables are enabled respectively, otherwise no message.
  • (none) or NOTICE: Important information is printed to stderr to draw the user's attention.
  • STATUS: Main news that may be of interest to project users. Ideally, these letters should be concise and to the point, no longer than one line, but still informative.
  • VERBOSE: Detailed information message for project users. These messages should provide additional details that are not of interest in most cases, but may be useful to those building the project when they want a deeper understanding of what is going on.
  • DEBUG: Detailed informational message for developers working on the project itself, rather than users who just want to build the project. These messages are typically not of interest to other users building the project, and are often closely related to internal implementation details.
  • TRACE: Fine-grained messaging with very low-level implementation details. Messages using this log level are typically only temporary and are expected to be deleted before publishing the project, packaging files, etc.

ADD_EXECUTABLE directive

Generate executable file

add_executable(hello ${src_list})
# hello是可执行文件名,后面是所需要的源文件

ADD_SUBDIRECTORY directive

add_subdirectory(src bin) # 源文件在src,生成的二进制文件在bin目录(在进行cmake命令的目录下会自行创建)

ADD_LIBRARY directive

set(share_list hello.cpp)
add_library(hello SHARED|STATIC ${share_list}) 
# hello是生成的第三方库的名称,但是最后的名称是libhello.so

SET_TARGET_PROPERTIES directive

Set some properties of the output file, such as the last name of the generated file

ADD_LIBRARY (hello_static STATIC ${LIBHELLO_SRC})
SET_TARGET_PROPERTIES (hello_static PROPERTIES OUTPUT_NAME "hello")

INCLUDE_DIRECTORIES directive

include_directories(path) # 添加头文件的搜索路径

LINK_DIRECTORIES command

link_directories(path) # 添加第三方二进制执行库的搜索路径

CMake internal build vs external build

The operation is to create a new build directory under the source file and enter the build directory. The intermediate files generated by the compilation and build process can be placed in the build folder

cmake ..  # 当前在的目录是编译路径,后面跟的是源文件目录
<projectname>_BINARY_DIR # 编译路径
<projectname>_SOURCE_DIR # 源文件路径

This internal and external statement should be relative to the source file.

CMake to install

Just install the generated binary files and help documents to the corresponding path.

install(TARGETS <target>... [...])             # 目标文件的安装,可执行二进制、动态库、静态库
install({FILES | PROGRAMS} <file>... [...])    # FILES普通文本文件,PROGRAMS非目标的可执行程序
install(DIRECTORY <dir>... [...])    # 将一个或多个目录的内容安装到给定的目的地。如果最后有/表示其中的内容而不包括目录本身
install(SCRIPT <file> [...])                   # 安装过程中调用给定的CMake脚本(.cmake脚本文件)
install(CODE <code> [...])                     # 安装过程中调用给定的CMake指令
install(EXPORT <export-name> [...])
# 在使用路径的时候如果使用相对路径,默认是/usr/local/路径,更改需要依照CMAKE_INSTALL_PREFIX变量
# cmake -DCMAKE_INSTALL_PREFIX=/usr ..

Example

➜  cmakestudy tree
.
|-- CMakeLists.txt
|-- build
`-- src
    |-- CMakeLists.txt
    `-- main.cpp

2 directories, 3 files
install(TARGETS hello DESTINATION src) # 将目标可执行文件hello安装到src目录中
cmake -DCMAKE_INSTALL_PREFIC=../ ..   # 在build目录下运行
make
make install

Guess you like

Origin blog.csdn.net/weixin_46287316/article/details/128606955