Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

c++ - Change default CMakeLists.txt in CLion to include warnings

In CLion the default CMakeLists.txt sets the C++11 compiler flag only. I'd like to have warnings and the pedantic flag by default in all my projects.

This is by default

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

and I'd like to have this by default so I don't have to change it every time I create a new project

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -W -Wall -Wextra -pedantic")

In the "Settings" - "File and Code Templates" I can change the C++ header/source template but not the CMakeLists.txt template. Is there any way I can change the default CMakeLists.txt template in CLion?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Be careful that this cannot be overridden by editing the CmakeLists.txt!

Also I recommend making a backup copy of the file in question.

Go to the location where you've installed/unzipped CLion and then

bin/cmake/share/cmake-3.2/Modules

and look for the file named "CMakeCXXInformation.cmake"

There just write/paste your flags following this model:

#This should be my default compiler flags
SET(CMAKE_CXX_FLAGS "-std=c++11 -Wall -Werror -Wextra -pedantic -
Wshadow -Woverloaded-virtual -Winvalid-pch -Wcast-align -Wformat=2 -
Wformat-nonliteral -Wmissing-declarations -Wmissing-format-attribute -
Wmissing-include-dirs -Wredundant-decls -Wswitch-default")

You can write/paste this wherever you'd like - top/bottom of the file.

Save the file, reload you Cmake Project. Enjoy!

(The line with the compiler flags set in CmakeList.txt can be ignored/deleted)

 

Probably a better solution:

Since this doesn't seem to be supported by any of Intellij's products, I think the best solution, if you want the code to still be portable is to use a [live template] (https://www.jetbrains.com/idea/help/live-templates.html)

I now use a live template called flags in which I just pasted my commonly used flags and also some other code tidbits. So whenever I am in CMakeLists.txt i can type flags and then press Tab to expand that text. You could of course have many versions of flags and using live templates for them will ease the burden of having to type them individually every time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...