当前位置:网站首页>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
边栏推荐
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
- Qt development experience tips 226-230
- OSPF notes [multiple access, two multicast addresses with OSPF]
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
- [today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
- Jenkins automatically cleans up construction history
- OdeInt與GPU
- Odeint and GPU
- Codeforces Round #771 (Div. 2) ABCD|E
猜你喜欢

2022危险化学品生产单位安全生产管理人员题库及答案

Applications and features of VR online exhibition

Basic usage, principle and details of session

Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I

【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码

Custom components in applets

One job hopping up 8K, three times in five years

VIM easy to use tutorial

测量三相永磁同步电机的交轴直轴电感

Mallbook: how can hotel enterprises break the situation in the post epidemic era?
随机推荐
Daily question - line 10
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
[leetcode skimming] February summary (updating)
The index is invalid
Dual contractual learning: text classification via label aware data augmentation reading notes
MySQL advanced -- you will have a new understanding of MySQL
2022 gas examination question bank and online simulation examination
嵌入式系统开发笔记81:使用Dialog组件设计提示对话框
Maixll dock quick start
Concurrent mode of different performance testing tools
什么是uid?什么是Auth?什么是验证器?
[godot] unity's animator is different from Godot's animplayer
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
2022年聚合工艺考试题及模拟考试
[pat (basic level) practice] - [simple simulation] 1064 friends
Codeworks round 449 (Div. 1) C. Kodori tree template
CUDA development and debugging tool
Knowledge supplement: redis' basic data types and corresponding commands
Task04 | statistiques mathématiques
TASK04|數理統計