当前位置:网站首页>Cmake selecting compilers and setting compiler options
Cmake selecting compilers and setting compiler options
2022-07-01 04:39:00 【Things turn at will】
One 、 Set the compiler version
Multiple versions of the compiler may be installed in the actual project platform , At the same time, different compilation parameters may need to be set for different functions , You can set the compiler version as follows
1、 Specify... Directly on the command line
cmake .. -DCMAKE_CXX_COMPILER=/usr/local/gcc/bin/g++
2、 Specified in the configuration file
set (CMAKE_C_COMPILER "/usr/local/gcc/bin/gcc")
set (CMAKE_CXX_COMPILER "/usr/local/gcc/bin/g++")
Modify global variables directly CMAKE_C_COMPILER and CMAKE_CXX_COMPILER Compiler path specified for . notes : These two commands should be placed at the beginning of the file (cmake_minimum_required Under command , Above other commands ), Otherwise it may not work .
CMAKE_C_COMPILER
It was originally to save environment variables "CC" Variable of value , and CC Is compiled C The preferred compiler for languages , But in the new CMP0054 If set in the policy CMAKE_C_COMPILER Will ignore CC Value .
CMAKE_CXX_COMPILER
And CMAKE_C_COMPILER similar , However, the environment variable corresponding to this variable is CXX, Is compiled C++ Compiler of language
Generated results

Two 、 Set compilation parameters
Suppose I use g++ compiler , add to "-std=c++11", “-Wall" and ”-Werror" Etc .
2.1 Use add_compile_options command
By means of CMakeLists.txt Add... To the file add_compile_options The command can be used to add parameters , Such as :
add_compile_options(-std=c++11 -Wall -Werror)But this command is for all types of compilers , In other words, the options added here will be used in all compilers , such as -std=c++11 Is aimed at C++ Compiler parameters for , It will also be used in C In the language compiler , Although it may not be wrong, it is not a good experience after all . And the parameters added by this command are recursive , That is, in a multi-layer directory structure , After setting options under the root file , All subdirectories will be compiled using .
2.2 By setting CMAKE_CXX_FLAGS To configure the
CMAKE_CXX_FLAGS Is aimed at C++ Compiler parameter options , Save environment variables by default CXX_FLAGS The content of , But if you modify the parameter value directly , Then the system will ignore the original CXX_FLAGS The content of . The setting method is as follows :
set(CMAKE_CXX_FLAGS
-std=c++11
-Wall
-Werror
)
This variable is only valid in the current file , If there are multiple modules in the project , Multiple compiled files , Then it needs to be in every CMakeLists.txt Add corresponding commands and parameters to the file .
The two ways are compared 
Sum up , For some compilation options that are common throughout the project, you can use add_compile_options Command to add more convenient , For the individual options in each module, use CMAKE_CXX_FLAGS Better variables .
3、 ... and 、CMAKE__FLAGS Variable
What we use here CMAKE_CXX_FLAGS Variables are only for C++ Compiler options , For other programming languages , Just replace the part , At present cmake edition (3.17.2) The following languages are supported in :
CMAKE_C_FLAGS:C Language compiler options , Corresponds to the environment variable CFLAGS
CMAKE_CXX_FLAGS:C++ Language compiler options , Corresponds to the environment variable CXXFLAGS
CMAKE_CUDA_FLAGS:CUDA Language compiler options , Corresponds to the environment variable CUDAFLAGS
CMAKE_Fortran_FLAGS:Fortran Language compiler options , Corresponds to the environment variable FFLAGS
Four 、 example
if (MSVC)
# warning level 4 and all warnings as errors
add_compile_options(/W4 /WX)
else()
# lots of warnings and all warnings as errors
add_compile_options(-Wall -Wextra -pedantic -Werror)
endif()
# Determine compiler type , If it is gcc compiler , Add... To the compilation options c++11 Support
if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
message(STATUS "optional:-std=c++11")
endif(CMAKE_COMPILER_IS_GNUCXX)
Reference resources :
cmake(5): Select compiler and set compiler options _ Xiangdi blog -CSDN Blog _cmake Set up compiler
边栏推荐
- Possible problems and solutions of using scroll view to implement slider view
- Why is Hong Kong server most suitable for overseas website construction
- 2022年上海市安全员C证考试题模拟考试题库及答案
- C language games (I) -- guessing games
- Knowledge supplement: basic usage of redis based on docker
- Task04 mathematical statistics
- js 图片路径转换base64格式
- Dual Contrastive Learning: Text Classification via Label-Aware Data Augmentation 阅读笔记
- After many job hopping, the monthly salary is equal to the annual salary of old colleagues
- Programs and processes, process management, foreground and background processes
猜你喜欢

2022 Shanghai safety officer C certificate examination question simulation examination question bank and answers

VIM简易使用教程

Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling

Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022

The junior college students were angry for 32 days, four rounds of interviews, five hours of soul torture, and won Ali's offer with tears

Software testing needs more and more talents. Why do you still not want to take this path?

This may be your last chance to join Tencent

Task04 | statistiques mathématiques

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

283. move zero
随机推荐
OdeInt与GPU
Maixll-Dock 快速上手
[Master / slave] router election in DD message
js 图片路径转换base64格式
[human version] Web3 privacy game in the dark forest
什么是uid?什么是Auth?什么是验证器?
Dual contractual learning: text classification via label aware data augmentation reading notes
Account sharing technology enables the farmers' market and reshapes the efficiency of transaction management services
[pat (basic level) practice] - [simple simulation] 1064 friends
网站服务器:好用的网站服务器怎么选这五方面要关注
25.k sets of flipped linked lists
Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
2022年煤气考试题库及在线模拟考试
嵌入式系统开发笔记81:使用Dialog组件设计提示对话框
Seven crimes of counting software R & D Efficiency
How to view the changes and opportunities in the construction of smart cities?
CF1638E colorful operations
2022年T电梯修理题库及模拟考试
软件研发的十大浪费:研发效能的另一面
Custom components in applets