当前位置:网站首页>Read cmake in one article
Read cmake in one article
2022-07-28 00:42:00 【CAir2】
This article is based on VS Angle description CMake The grammatical . Specific grammatical correspondence , Refer to the following table .
CMake Function corresponds to VS Related settings
| VS Related settings | CMake function |
|---|---|
| Specify the directory containing header files | target_include_directories |
| Specify the link library Directory | target_link_directories |
| Specify the link library | target_link_libraries |
| Appoint C++ standard | set(CMAKE_CXX_STANDARD 11) |
| Specify compiler version | cmake -G “Visual Studio 12 2013” |
| Specify platform set | cmake -G “Visual Studio 12 2013 Win64” |
| Specify the compilation type | set(CMAKE_BUILD_TYPE “Debug”) Equivalent to g++ -g |
| Specify precompiled macros | add_definitions(-Dxxx -Dyyy ) must -D start |
| Specify the solution name | project(xxxx) |
CMake common Built-in variables
| CMake Built-in variables | explain ( All paths are full paths ) |
|---|---|
| PROJECT_NAME | CMakeList.txt In the setting of project_name |
| PROJECT_SOURCE_DIR | root CMakeLists.txt Path |
| PROJECT_BINARY_DIR | Construction directory of the project (build Catalog ) |
| CMAKE_CURRENT_SOURCE_DIR | At present ’CMakeLists.txt’ Path |
| CMAKE_CURRENT_BINARY_DIR | Currently in process ’ Build directory ’ |
| CMAKE_CURRENT_LIST_DIR | What is being processed ’cmake file ’ directory |
| CMAKE_CURRENT_LIST_FILE | What is being processed ’CMakeLists.txt or cmake’ The full path of the file |
| CMAKE_CURRENT_LIST_LINE | What is being processed ’CMakeLists.txt or cmake’ Of documents ’ Line number ’ |
| CMAKE_PROJECT_NAME | Whole project ’ Configured project_name |
| LIBRARY_OUTPUT_DIR | The directory where the library is stored ( User settings are required ) |
| BINARY_OUTPUT_DIR | The storage directory of executable files ( User settings are required ) |
CMake Hellow word
project
project/main.cpp
project/CMakeLists.txt
project/build
project/CMakeLists.txt
# Set up CMake Version for
cmake_minimum_required(VERSION 3.10)
# Set project name
project(hellow)
# Set up C++11 standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
# Add executable
add_executable(hellow main.cpp)
perform CMake Instructions
# Current directory build
#-G Specify compiler Vs2013 And platform toolset types X64
cmake -G "Visual Studio 12 2013 Win64" ../

Appoint Debug Pattern (-g)
set(CMAKE_BUILD_TYPE "Debug")
Generate mymath library add_library
project/mymath
project/mymath/mymath.h
project/mymath/mymath.cpp
project/mymath/CMakeLists.txt
project/CMakeLists.txt
# Add a subdirectory to the compilation project , Subdirectories should contain CMakeLists.txt
add_subdirectory(mymath)
# Add header file containing directory
target_include_directories(hellow PUBLIC
"${PROJECT_SOURCE_DIR}/mymath"
)
# link lib
target_link_libraries(hellow PUBLIC mymath)
project/mymath/CMakeLists.txt
# add to lib The default is static library , If specified SHARED Dynamic library
add_library(mymath SHARED mymath.cpp)
This will generate mymath The library , The following is complete project/CMakeLists.txt
# Set up CMake Version for
cmake_minimum_required(VERSION 3.10)
# Set project name
project(hellow)
# Set up C++11 standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
# Add a subdirectory to the compilation project , Subdirectories should contain CMakeLists.txt
add_subdirectory(mymath)
# Add executable
add_executable(hellow main.cpp)
# Add header file containing directory
target_include_directories(hellow PUBLIC
"${PROJECT_SOURCE_DIR}/mymath"
)
# link lib
target_link_libraries(hellow PUBLIC mymath)

Link third party libraries
Link below d://test//test.lib For example ,test.lib The header file of is in d://test//include//
# additional test.lib
target_link_libraries(hellow PUBLIC mymath test)
# Appoint lib Warehouse location
target_link_directories(hellow PUBLIC "d:/test")
# Appoint lib Header file location
target_include_directories(hellow PUBLIC
"d:/test/include"
"${PROJECT_SOURCE_DIR}/mymath"
)
The following is complete project/CMakeLists.txt
# Set up CMake Version for
cmake_minimum_required(VERSION 3.10)
# Set project name
project(hellow)
# Set up C++11 standard
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED true)
# Add a subdirectory to the compilation project , Subdirectories should contain CMakeLists.txt
add_subdirectory(mymath)
# Add executable
add_executable(hellow main.cpp)
# Add header file containing directory
target_include_directories(hellow PUBLIC
"d:/test/include"
"${PROJECT_SOURCE_DIR}/mymath"
)
# link lib
target_link_libraries(hellow PUBLIC mymath test)
# Specify the directory of the link library
target_link_directories(hellow PUBLIC "d:/test")

Add predefined macros add_definitions
for example : Must define
USE_OPEN_SSLAbility to open openssl
# Add predefined macros , must -D start
add_definitions(-DUSE_OPEN_SLL)
Multi document inclusion processing
If the target contains multiple files , If you specify it manually, it will be troublesome . Can pass aux_source_directory Scan and get the current directory file .
# increase sub function
project/sub.cpp
project/sub.h
# Scan the current directory file , And saved in variable all_file in
aux_source_directory("${CMAKE_CURRENT_SOURCE_DIR}" all_file)
add_executable(hellow ${
all_file})
Configure the project version number and information
# Set the project name and version
project(Tutorial VERSION 1.0)
# Configure the header file to pass the version number to the source code
# Configure macro definition , The default value of configuration is ON, If you want to close cmake -DUSE_MYMATH=OFF
#option except ON Everything else defaults to OFF
option(USE_MYMATH "user mymath.lib" ON)
# Pass a string to the configuration file
set(var_Key "hellow word")
# Pass on INT Give the configuration file
set(INT_Key 1)
# Input file The output file
configure_file(hellow_ver.h.in hellow_ver.h)
# Due to the configured file hellow_ver.h Will be written to the binary directory
# So we have to add this directory to include File search path
target_include_directories(hellow PUBLIC
"d:/test/include"
"${PROJECT_SOURCE_DIR}/mymath"
"${PROJECT_BINARY_DIR}"
)
hellow_ver.h.in
// Configured project version number
#define hellow_VERSION_MAJOR @[email protected]
#define hellow_VERSION_MINOR @[email protected]
// Macro definition
#cmakedefine USE_MYMATH
// Be careful :${} The name between should be the same as cmakedefine The following variable names are the same
// Variable name and cmakeLists.txt Set the same variable name
#cmakedefine var_Key "@[email protected]”
#cmakedefine INT_Key @[email protected]
Generated after running hellow_ver.h
If you want to cancel USE_MYMATH have access to -DUSE_MYMATH=OFF
cmake -G "Visual Studio 12 2013 Win64" ../ -DUSE_MYMATH=OFF
Generate dynamic library or Static library
# determine mymath.lib Whether to use static library
option(MYMATH_STATIC "user mymath static lib" ON)
if(MYMATH_STATIC)
add_library(mymath STATIC mymath.cpp)
else()
add_library(mymath SHARED mymath.cpp)
endif()
In this paper, the reference :CMake course , as for CMake The installation and testing of will be explained in the next chapter .
Function description
about CMake, There may be two functions :xxxxx and target_xxxx. Let's say
link_librariesandtarget_link_librariesTo explain .link_libraries: Global , namely :CMake All modules of will add this library .target_link_libraries: Targeted , Only the specified target contains the Library .
for example CMake Including multiple goalsAandB, At this point, if you passlink_libraries(ws2_32 test)containws2_32.libandtest.libthatAandBThese two libraries will be included . But if it goes throughtarget_link_libraries(A PUBLIC ws2_32 test)Then at this time, only the goal A Contains these two libraries .
边栏推荐
- Leetcode 415. string addition and 43. string multiplication
- Openvino integrates tensorflow to accelerate reasoning
- 特权更改对现有连接的影响
- Matlab | those matlab tips you have to know (3)
- Smart convenience store takes you to unlock the future technology shopping experience
- 大众中国豪掷80亿,成国轩高科第一大股东
- 49 times faster, NVIDIA releases quantum hybrid programming platform qoda
- See how well-known enterprises use Web3 to reshape their industries
- 小程序助力智能家居生态平台
- 相应通道无电压但ADC的值却在大幅变化且不等于0的可能原因
猜你喜欢

Smart convenience store takes you to unlock the future technology shopping experience

Matlab | those matlab tips you have to know (4)

从第二层到第三层

The second uncle cured my spiritual internal friction and made me angry out of station B

公司7月来了个软件测试工程师,一副毛头小子的样儿,哪想到是新一代卷王...

Oracle password expiration solution

MATLAB | 那些你不得不知道的MATLAB小技巧(四)

二舅治好我的精神内耗,也让我火出了B站

【打新必读】魅视科技估值分析,分布式视听产品及解决方案

Build Release Blogs
随机推荐
Yangchuanhui, CTO of oceanbase: some HTAP databases are not real htaps
Jmeter 如何解决乱码问题?
Intel AI practice day issue 56 | explore new trends in industry development
MATLAB | 那些你不得不知道的MATLAB小技巧(二)
Build Release Blogs
Threejs personal notes
A design scheme of Wal
What foundation does Yolo need? How to learn Yolo?
理解双亲委派模式
Shell(3)
CSDN21天学习挑战赛
LeetCode_位运算_中等_137.只出现一次的数字 II
Server open sensitive port
Promoting cloud network integration and building a digital economy: Intel unveiled the 5th Digital China Construction Summit - cloud ecosystem Conference
永州出入境检验实验室建设那些事
Read one line of string input each time (to be supplemented)
MFC prompts that this application has requested the runtime to terminate it in an unused way editbox box has been deleted and is still in use
自动推理的逻辑07–谓词演算
How does matlab set the K-line diagram to classic red and green color matching?
小程序助力智能家居生态平台