当前位置:网站首页>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 .
边栏推荐
- LM08丨网格系列之网格反转(精)
- Redisson uses the full solution - redisson official document + comments (Part 2)
- 【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
- Day5: scanner object, next() and nextline(), sequential structure, selection structure, circular structure
- 【R语言】两个/N个数据合并merge函数
- ContentType所有类型对比
- 长路漫漫、技术作伴
- PWN攻防世界int_overflow
- 2022 electrician (intermediate) recurrent training question bank and answers
- TCP/UDP 通信问题整理
猜你喜欢

QT -- 1. QT connection database

Why some people earn nearly 10billion a year, while others earn 3000 a month: the details you ignore actually make the most money

Eigen matrix operation Library

Redisson uses the full solution - redisson official document + comments (Part 2)

Download xshell and xftp

Cyclic neural network

图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证

window c盘满了

2022 test question bank and simulation test of tea master (primary) operation certificate

Basic knowledge of MATLAB
随机推荐
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Caesar
Gui Gui programming (XV) - use scale to control font size changes
Conscience Amway universal wheel SolidWorks model material website
matlab保存DB4i深度相机图片
2022 electrician (intermediate) recurrent training question bank and answers
2022茶艺师(初级)操作证考试题库及模拟考试
The triode is a great invention
2022 test questions and mock examinations for main principals of hazardous chemicals business units
Significance and measures of source code encryption
力扣每日一题-第31天-202.快乐数
关系数据库如何工作
She is the "HR of others" | ones character
[untitled]
[软件] phantomjs屏幕截图
Vhost kick & call principle
[MySQL learning notes 28] storage function
Eigen矩阵运算库快速上手
【无标题】
Introduction to kubernetes resource objects and common commands (II)