当前位置:网站首页>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 .
边栏推荐
- 2022 electrician (intermediate) recurrent training question bank and answers
- Conscience Amway universal wheel SolidWorks model material website
- AArdio - 【问题】bass库回调时内存增长的问题
- [skill] create Bat quick open web page
- [R language] two /n data merge functions
- Principle and process of embossing
- sqlalchemy创建MySQL_Table
- Introduction to kubernetes resource objects and common commands (II)
- 长路漫漫、技术作伴
- PWN攻防世界int_overflow
猜你喜欢

Minecraft 1.16.5 module development (51) tile entity

Atguigu---- scaffold --02- use scaffold (2)

MATLAB之基础知识

Vhost kick & call principle
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario

2022广东省安全员A证第三批(主要负责人)特种作业证考试题库模拟考试平台操作

STM32 uses esp01s to go to the cloud, mqtt FX debugging

kubernetes资源对象介绍及常用命令(二)

IMDB practice of emotion classification (simplernn, LSTM, Gru)

良心安利万向轮 SolidWorks模型素材网站
随机推荐
Huawei modelarts training alexnet model
2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
2022茶艺师(初级)操作证考试题库及模拟考试
Aardio - Shadow Gradient Text
Gui Gui programming (XV) - use scale to control font size changes
Scala语言学习-07-构造器
【入门】截取字符串
Lm08 mesh series mesh inversion (fine)
LSTM of RNN
SharePoint - modify web application authentication using PowerShell
Vhost kick & call principle
[MySQL learning notes 25] SQL statement optimization
【力扣10天SQL入门】Day10 控制流
Principle and process of embossing
奥迪AUDI EDI 项目中供应商需要了解哪些信息?
I bet on performance and won the CTO of the company. I want to build Devops platform!
Caesar
Kickback -- find the first palindrome character in a group of characters
STM32 uses esp01s to go to the cloud, mqtt FX debugging
良心安利万向轮 SolidWorks模型素材网站