# Header guards

[Header guard](https://en.wikipedia.org/wiki/Include_guard) is a pattern of preprocessor directives that protect your header from being included multiple times. Header guard wraps the entire code content into an `#ifndef` (`#if !defined`, or another similar) block:

```CPLUSPLUS
#ifndef MY_HEADER_H
#define MY_HEADER_H
    //...
#endif
```

Procedure: Edit the header file templates

By default, header guards are included in the [file templates](using-file-and-code-templates.html) that specify the initial content for new headers. You can edit these templates if, for example, you decide to use [#pragma once](https://en.wikipedia.org/wiki/Pragma_once) instead of header guards. For this, go to `Settings | Editor | File and Code Templates` and open the Files tab. Select C Header File or C++ Class Header from the list, and change the template content:

![header guard in file template](https://resources.jetbrains.com/help/img/idea/2026.2/cl_headerguard_filetemplate.png)

Notice the [literal](http://velocity.apache.org/engine/1.7/user-guide.html#literals) syntax wrapping `#pragma once`. For file templates, CLion uses the [Apache Velocity](http://velocity.apache.org/engine/devel/user-guide.html#Velocity_Template_Language_VTL:_An_Introduction) language, which requires escaping for the C/C++ preprocessor directives in order to parse them correctly.

Procedure: Configure the header guard symbol

1. Header guard symbol (`MY_HEADER_H` from the example above) should be unique, so it usually relates to the filename. To configure the pattern for header guard symbol, open the [Naming Convention](naming-conventions.html) tab in `Settings | Editor | Code Style | C/C++` and specify the template in the Header Guard field:

![header guard field in naming convention settings](https://resources.jetbrains.com/help/img/idea/2026.2/cl_headerguard_template.png)

2. You can use various predefined variables, for example:

* `${PROJECT_NAME}` - the name of the current project.

* `${PROJECT_REL_PATH}` - the relative target path. For instance, if the project is located in the `prj` directory, and the target file is `prj/src/dir/header.h`, then `${PROJECT_REL_PATH}` will be equal to `src/dir`.

* `${FILE_NAME}` - the target filename without extension.

* `${EXT}` - the target file extension.

* Other variables that you can find in [file templates](file-template-variables.html#predefined_template_variables), such as `${USER}` or `${DATE}`.

A valid header guard symbol can contain the following characters: upper case `'A-Z'` and lower case `'a-z'` letters, the underscore sign `_`, digits (but cannot start with a digit), and the dollar sign `$`. Note that CLion appends an `INC_` prefix to symbols that start with digits, and replaces invalid symbols using the `INC_${UUID}` pattern.

3. The configured symbol replaces the `${INCLUDE_GUARD}` variable in the header templates. CLion will apply the pattern when creating new C/C++ classes and header files. Also, if you [rename](rename-refactorings.html) a class or a file, the header guards matching the currently configured style will be updated with the new name:

![automatic changes in header guard template for file rename](https://resources.jetbrains.com/help/img/idea/2026.2/cl_headerguard_rename.animated.gif)

