简体   繁体   English

更改CLion中的默认CMakeLists.txt以包含警告

[英]Change default CMakeLists.txt in CLion to include warnings

In CLion the default CMakeLists.txt sets the C++11 compiler flag only. 在CLion中,默认的CMakeLists.txt仅设置C ++ 11编译器标志。 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. 在“设置” - “文件和代码模板”中,我可以更改C ++标头/源模板,但不能更改CMakeLists.txt模板。 Is there any way I can change the default CMakeLists.txt template in CLion? 有什么办法可以在CLion中更改默认的CMakeLists.txt模板吗?

Be careful that this cannot be overridden by editing the CmakeLists.txt! 注意不能通过编辑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 转到已安装/解压缩CLion的位置,然后转到

bin/cmake/share/cmake-3.2/Modules

and look for the file named "CMakeCXXInformation.cmake" 并查找名为“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. 保存文件,重新加载Cmake Project。 Enjoy! 请享用!

(The line with the compiler flags set in CmakeList.txt can be ignored/deleted) (可以忽略/删除CmakeList.txt中设置的编译器标志的行)

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 ) 由于Intellij的任何产品似乎都不支持这一点,我认为最好的解决方案是,如果您希望代码仍然可移植,则使用[实时模板]( 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. 我现在使用一个名为flags的实时模板,其中我只粘贴了我常用的标志以及其他一些代码花絮。 So whenever I am in CMakeLists.txt i can type flags and then press Tab to expand that text. 因此,每当我在CMakeLists.txt中时,我都可以键入标志,然后按Tab键展开该文本。 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. 您当然可以拥有许多版本的标志,并且使用实时模板可以减轻每次单独键入它们的负担。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM