当前位置:网站首页>Cmake I two ways to compile source files
Cmake I two ways to compile source files
2022-07-01 07:57:00 【The user hasn't thought of a good nickname yet】
Catalog
Two 、check__source_compiles modular
try_compile and check_<LANG>_source_compiles Can compile source files , And link it to the executable .
One 、try_compile command
Enter at the command line cmake --help-command try_compile You can get try_compile file .
command try_compile It provides a more complete interface and two different operation modes :
1. With a complete CMake Project as input , And based on its CMakeLists.txt To configure 、 Build and link . Such operation Patterns provide greater flexibility , Because the complexity of the project to be compiled is optional .
2. Source files are provided , And for containing directories 、 Configuration options for link libraries and compiler flags .
therefore , try_compile Based on calling... On the project CMake, among CMakeLists.txt Already exist ( In the first operation mode In style ), Or based on passing to try_compile Parameter dynamic generation file .
1. Compile the entire project
try_compile(<resultVar> <bindir> <srcdir> <projectName> [<targetName>] [CMAKE_FLAGS <flags>...] [OUTPUT_VARIABLE <var>])
- resultVar: Save the compilation results .
- bindir:try_compile Intermediate files will be generated in this directory .
- srcdir: contains CMakeLists.txt And the integrity of the source code CMake project .
- projectName: Project name .
- targetName: Appoint <targetName> To generate a specific target , instead of all or ALL_BUILD .
Be careful : In the current format , Running try_compile After the command ,<bindir> and <srcdir> It won't be deleted .
2. Compile source file
try_compile(<resultVar> <bindir> <srcfile|SOURCES srcfile...> [CMAKE_FLAGS <flags>...] [COMPILE_DEFINITIONS <defs>...] [LINK_OPTIONS <options>...] [LINK_LIBRARIES <libs>...] [OUTPUT_VARIABLE <var>] [COPY_FILE <fileName> [COPY_FILE_ERROR <var>]] [<LANG>_STANDARD <std>] [<LANG>_STANDARD_REQUIRED <bool>] [<LANG>_EXTENSIONS <bool>] )Be careful : When using the current format ,<bindir>/CMakeFiles/CMakeTmp The files in the directory will be deleted automatically . Use --debug-trycompile This behavior can be avoided .
# Set up a temporary directory , try_compile Intermediate files will be generated under this directory
set(_scratch_dir ${CMAKE_CURRENT_BINARY_DIR}/omp_try_compile)
# Try compiling the source file taskloop.cpp
try_compile(
omp_taskloop_test_1 # Save the compilation results
${_scratch_dir}
SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/taskloop.cpp
COMPILE_DEFINITIONS
${OpenMP_CXX_FLAGS}
LINK_LIBRARIES
${OpenMP_CXX_LIBRARIES}
)
message(STATUS "Result of try_compile: ${omp_taskloop_test_1}")If there are more than one in the code try_compile call , Only one can be debugged at a time :
1. function CMake, Don't use --debug-trycompile , Will run all try_compile command , And clean their Execute directories and files .
2. from CMake Delete the variables that hold the inspection results from the cache . Cache saved to CMakeCache.txt In file . To clear variables The content of , have access to -U Of CLI switch , Followed by the name of the variable , It will be interpreted as a global expression , So you can use * and ? :
cmake -U <variable-name>
3. Run again CMake, Use --debug-trycompile . Only the check to clear the cache will be rerun . The execution directory and files will not be cleaned up this time .
Two 、check_<LANG>_source_compiles modular
Enter at the command line cmake --help-module CheckCXXSourceCompiles You can get check_cxx_source_compiles file .
check_cxx_source_compiles(<code> <resultVar> [FAIL_REGEX <regex1> [<regex2>...]])
check_<lang>_source_compiles The order is try_compile Simplified packaging of commands . therefore , It provides an interface :
1. The code snippet to compile must be as CMake The variable passed in . Most of the time , This means that you must use file(READ...) To read the file . Code snippets are saved in the build directory CMakeFiles/CMakeTmp Subdirectory .
2. Fine tune compilation and linking , The following must be set CMake Variables to :
- CMAKE_REQUIRED_FLAGS: Set compiler flags .
- CMAKE_REQUIRED_DEFINITIONS: Set precompiled macros .
- CMAKE_REQUIRED_INCLUDES: Set include directory list .
- CMAKE_REQUIRED_LIBRARIES: Set the list of libraries that the executable target can connect .
3. call check_<lang>_compiles_function after , You must manually unset these variables , To ensure that it is used later , The current content will not be preserved .
To use check_cxx_source_compiles function , Need to include CheckCXXSourceCompiles.cmake Module file :
include(CheckCXXSourceCompiles)
# Copy taskloop The contents of the source file to _snippet
file(READ ${CMAKE_CURRENT_SOURCE_DIR}/taskloop.cpp _snippet)
set(CMAKE_REQUIRED_FLAGS ${OpenMP_CXX_FLAGS})
set(CMAKE_REQUIRED_LIBRARIES ${OpenMP_CXX_LIBRARIES})
# Use code snippets as parameters , call check_cxx_source_compiles function . The inspection results will be saved to omp_taskloop_test_2 variable :
check_cxx_source_compiles("${_snippet}" omp_taskloop_test_2)
# Unset variables
unset(CMAKE_REQUIRED_FLAGS)
unset(CMAKE_REQUIRED_LIBRARIES)
message(STATUS "Result of check_cxx_source_compiles: ${omp_taskloop_test_2}")3、 ... and 、 summary
try_compile Provides a flexible and clean interface , Especially when the compiled code is not a short code . It is recommended that when testing compilation , Small code snippets are used check_<LANG>_source_compile . In other cases , choice try_compile .
边栏推荐
- Gui Gui programming (XV) - use scale to control font size changes
- Missing API interface actual development series (14): ID card real name authentication verification
- 良心安利万向轮 SolidWorks模型素材网站
- 【mysql学习笔记25】sql语句优化
- Redisson uses the complete solution - redisson official documents + Notes (Part 1)
- SharePoint - modify web application authentication using PowerShell
- Conscience Amway universal wheel SolidWorks model material website
- Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money
- go通用动态重试机制解决方案的实现与封装
- The database is locked. Is there a solution
猜你喜欢

window c盘满了

Redisson uses the complete solution - redisson official documents + Notes (Part 1)

Eigen矩阵运算库快速上手

Aardio - 阴影渐变文字

Conscience Amway universal wheel SolidWorks model material website

2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation

良心安利万向轮 SolidWorks模型素材网站

How relational databases work

Introduction to kubernetes resource objects and common commands (II)

【无标题】
随机推荐
[MySQL learning notes 28] storage function
[MySQL learning notes 25] SQL statement optimization
Teach you how to apply for domestic trademark online step by step
She is the "HR of others" | ones character
【入门】截取字符串
php laravel微信支付
[untitled]
How to make the two financial transactions faster
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
【无标题】
Discussion on several research hotspots of cvpr2022
2022 test questions and mock examinations for main principals of hazardous chemicals business units
力扣每日一题-第32天-1822.数组元素积的符号
【无标题】
038 network security JS
How outlook puts together messages with the same discussion
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
Scala语言学习-07-构造器
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
go通用动态重试机制解决方案的实现与封装