How to make CLion use "#pragma once" instead of "ifndef ... def ..." by default for new header files?
Asked Answered
W

1

23

By default, CLion will add the following lines to a newly created header file:

#ifndef SOME_NAME_H
#define SOME_NAME_H
.... your code here
#endif //SOME_NAME_H

But I like #pragma once more. How can I configure CLion so that it uses #pragma once by default for new header files?

Whine answered 22/7, 2015 at 18:1 Comment(2)
This question can be tagged with include-guard.Cytoplast
I found instructions for this on their site.Fewell
J
31

Go to File-> Settings -> Editor -> File and Code Templates. You will find there 3 tabs, namely, Templates, Includes, and Code. Now under Templates choose for example C Header File. Insert #pragma once to replace its content. Now every time you add a new Header from project menu you will have your template.

Jerrome answered 23/7, 2015 at 7:44 Comment(5)
How should I modify this so that it uses #pragma once?Whine
Note: #pragma is invalid Apache Velocity template because #foo will be parsed as a directive named foo. It's safer to use literal syntax here, like #[[#pragma]]# once.Cytoplast
Or escape the directive like \#pragma once.Cytoplast
For CLion 2021, use CLion > Preferences... > Editor > File and Code Templates > C++ Class Header.Undergrowth
@FranklinYu It's impressive how something so convenient as #pragma once finds so much opposition. Named include guards are incredibly error prone, I wouldn't put it in any proximity of the term "safer".Stinkwood

© 2022 - 2024 — McMap. All rights reserved.