当前位置:网站首页>Notes on cmake compilation tool
Notes on cmake compilation tool
2022-07-23 23:41:00 【xiaoma_ bk】
List of articles
cmake
cmake On
What is? CMAKE
- CMake It's a cross platform compilation tool .
- First write CMakeLists.txt file , And then through cmake The order will CMakeLists.txt Files are converted into executable files of the corresponding platform make file , Last use make Command compile source code to generate executable program or library file , So as to avoid different platforms Makefile The problem of format .
C/C++ Compilation process
The compilation process is actually a language ( It's usually a high-level language ) Translate into another language ( It's usually a low-level language ). C/C++ The main process of program compilation : Source code → The preprocessor → compiler → Assembler → The linker → Executable program .
Preprocessing : C/C++ in , Before the compiler compiles the source file , First, it should be pretreated .
- The preprocessor provides a set of precompiled processing instructions and preprocessing operators , Formally # start , Actually, it doesn't belong to C/C++ The statement in , Cannot be translated by compiler , You need to do a preprocessing before the real compilation , Last output one “.i” file .
compile : Compiling is to put C/C++ Convert code to assembly code .
- The compiler needs to pass lexical analysis and syntax analysis , After confirming that all instructions conform to the syntax rules and there are no syntax errors , Convert code into assembly language , Generate assembly code .
assembly : Assembly is the process of translating the assembly code output in the previous step into the target machine instructions , The generated object file is a machine language equivalent to the source program .
link : The link is the object file generated by the assembly 、 The target file of the system library 、 Linking library files , Generate executable programs that can run on a specific platform .
CMake Common variables
CMAKE_BINARY_DIR、PROJECT_BINARY_DIR、<projectname>_BINARY_DIR- These three variables refer to the same content , If it is in source compile , Refers to the project top-level directory ,
- If it is out-of-source compile , It refers to the directory where project compilation occurs .
- PROJECT_BINARY_DIR Slightly different from other instructions , Now? , You can understand that they are consistent .
CMAKE_SOURCE_DIR、PROJECT_SOURCE_DIR、<projectname>_SOURCE_DIR- These three variables refer to the same content , No matter how you compile it , It's all the top-level catalogue of the project .
- stay in source Compile time , He followed
CMAKE_BINARY_DIRAnd so on .
CMAKE_CURRENT_SOURCE_DIR- Refers to the current processing of CMakeLists.txt Path , For example, we mentioned above src subdirectories .
CMAKE_CURRRENT_BINARY_DIR- If it is in-source compile , It goes with CMAKE_CURRENT_SOURCE_DIR Agreement , If it is out-of-source compile , He means target Compile Directory .
- Use the
ADD_SUBDIRECTORY(src bin)You can change the value of this variable . - Use
SET(EXECUTABLE_OUTPUT_PATH < New path >)It doesn't affect this variable , It only changes the path where the final target file is stored .
CMAKE_MODULE_PATH- This variable is used to define its own cmake The path of the module .
- Eg:
SET(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake), And then you can go through INCLUDE Command to call your own module .
EXECUTABLE_OUTPUT_PATHandLIBRARY_OUTPUT_PATH- They are used to redefine the storage directory of the final results , We mentioned these two variables earlier .
How to write cmake
cmake_minimum_required(VERSION 3.16): Is used to specify the cmake Minimum version requirements ; project(projectName) : Used to specify the project name ; include_directories() : Used to import header file search path ; aux_source_directory(src dir_srcs): Used to include the source file directory ; set(TEST_MATH) : Used to set environment variables , All the source files used for compilation should be put here ; add_executable(${PROJECT_NAME} ${TEST_MATH}): Used to add executable files to compile ; target_link_libraries(${PROJECT_NAME} m): It is used to add the library required by the executable ;New variable , Store source files :
set (SRC_LIST ./main.c ./func.c ./func1.c)
Multiple source files in different directories
- Add the search path of the specified header file
include_directories (func1 func2)
- Search all source files in the specified path , And save it in the variable
aux_source_directory (func1 SRC_LIST1)
- Add the search path of the specified header file
Add a subdirectory to the current project where the source files are stored
add_subdirectory (src)
Store the generated executable file in bin Under the table of contents
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
Links to Libraries
- Save the specified library file in the specified directory to the variable
FUNC_LIBinfind_library(FUNC_LIB func HINTS ${PROJECT_SOURCE_DIR}/func/lib)find_library: Find the specified library in the specified directory , And store the absolute path of the library in the variable , Parameter 1 is the variable name , The second parameter is the library name , The third parameter is HINTS, Parameter 4 is path .- Use
find_libraryThe advantage is to performcmake ..Will find out whether the library exists , In this way, errors can be found in advance , Don't wait for the link .
- Save the specified library file in the specified directory to the variable
Add compile options :
- Sometimes you want to add some compilation options when compiling a program , Such as -Wall,-std=c++11 etc. , You can use add_compile_options To operate .
add_compile_options(-std=c++11 -Wall)- add to C++ 11 Standard support , And let the compiler warn the code as much as possible
Add control options
option command : Parameters 1 by option Name , Parameters 2 For the string , describe option The function of , Parameters 3 by option Value ,ON or OFF, You can also leave it blank. The default is OFF.
First of all :CMakeList Conditional compilation
option(MYDEBUG "enable debug compilation" OFF)if (MYDEBUG) add_executable(main2 main2.c) else() message(STATUS "Currently is not in debug mode") endif()
cmake .. -DMYDEBUG=ONcommand control
second : Conditional compilation in code
CMakeListAddoption(WWW1 "print one message" OFF) # here `OFF、False` `ON、True` Fine if (WWW1) add_compile_options(-DWWW1) endif()cmake China and Canada this , The purpose is to use this variable in the code
*.hor*.cppin Case study :#ifdef WWW1 printf("--- define WWW1\n"); #else printf("---- not define WWW1\n"); #endif
Cmake Advanced
//TODO
边栏推荐
- Software architecture
- 怎么开户买收益6%的理财产品呢?
- [computer three-level information security] access control model
- Chapter 4: implementation use cases
- The title of solo article will filter out some tags
- Structured Streaming 编程模型(Input Table、Result Table、Output Mode...)
- Application of merging and sorting thought
- Tensorflow one layer neural network training handwritten digit recognition
- Is Zhongyuan securities reliable? Is it legal? Is it safe to open a stock account?
- C # introductory series (XXVIII) -- query syntax of LINQ
猜你喜欢
随机推荐
汇编语言伪指令详解(附实例)
cannot meet the needs of the people? How can programmers take private jobs to effectively increase their income?
This article will show you what typescript is
Esp8266 nodemcu - get real-time weather from Suning API
【攻防世界WEB】难度五星15分进阶题:bug
BUUCTF -rip
Galaxy Securities opens an account online. Is it safe to open an account on your mobile phone
[ssm] joint debugging of front and rear protocols ①
Principal component analysis (matlab)
solo 文章标题会过滤掉部分标签
Iptables prevent nmap scanning and binlog
Redis集群搭建(Cluster 集群模式,分片集群)
Stm32mp1 M4 bare metal cubeide Development Guide Chapter 6 stm32cube firmware package
PushGateway+Prometheus+Grafana构建Flink实时监控
Open source embedded sig in the openeuler community. Let's talk about its multi OS hybrid deployment framework
Is it safe to find an account manager to open a fund account online??
[tensorflow] check whether tensorflow GPU is available
bjdctf_2020_babystack
ciscn_ 2019_ c_ one
jarvisoj_level0


![[SSM]前后台协议联调②](/img/ed/6a42e19f384fa0d4a039e52f5a95f4.png)






