22

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?

a06e
  • 18,594
  • 33
  • 93
  • 169

1 Answers1

29

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.

Codoka
  • 836
  • 10
  • 11
  • How should I modify this so that it uses `#pragma once`? – a06e Jul 23 '15 at 18:06
  • 11
    Note: `#pragma` is invalid [Apache Velocity](http://velocity.apache.org/) template because `#foo` will be parsed as a directive named `foo`. It's safer to use [literal syntax](http://velocity.apache.org/engine/1.7/user-guide.html#literals) here, like `#[[#pragma]]# once`. – Franklin Yu Jan 06 '17 at 04:31
  • 1
    Or [escape the directive](http://velocity.apache.org/engine/1.7/user-guide.html#escaping-vtl-directives) like `\#pragma once`. – Franklin Yu Jan 06 '17 at 04:45
  • 2
    For `CLion 2021, use CLion > Preferences... > Editor > File and Code Templates > C++ Class Header`. – Bill Gale Sep 16 '21 at 20:18