当前位置:网站首页>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 .
边栏推荐
- Long way to go with technology
- How to get a SharePoint online site created using the office365 group template
- 2022电工(中级)复训题库及答案
- How relational databases work
- She is the "HR of others" | ones character
- [kv260] generate chip temperature curve with xadc
- 论文学习——水文时间序列相似性查询的分析与研究
- H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
- Eigen matrix operation Library
- [R language] two /n data merge functions
猜你喜欢
ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
What information does the supplier need to know about Audi EDI project?
[untitled]
Cyclic neural network
2022制冷与空调设备运行操作国家题库模拟考试平台操作
Aardio - Shadow Gradient Text
Teach you how to apply for domestic trademark online step by step
Basic knowledge of MATLAB
Office365 - how to use stream app to watch offline files at any time
随机推荐
Cyclic neural network
凸印的印刷原理及工艺介绍
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
sqlalchemy创建MySQL_Table
Thesis learning -- Analysis and Research on similarity query of hydrological time series
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
base64
[kv260] generate chip temperature curve with xadc
【技能】创建.bat快速打开网页
038 network security JS
How to troubleshoot SharePoint online map network drive failure?
Aardio - Shadow Gradient Text
Source code analysis of open source API gateway APIs IX
【入门】输入n个整数,输出其中最小的k个
Basic knowledge of MATLAB
Browser local storage
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
I bet on performance and won the CTO of the company. I want to build Devops platform!
Minecraft 1.16.5模组开发(五十一) 方块实体 (Tile Entity)