Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change C++ include guards in CLion?

When CLion creates a header file it adds include guard strings like this:

#ifndef PROJECTNAME_FILENAME_H
#define PROJECTNAME_FILENAME_H

/* ... code ... */

#endif //PROJECTNAME_FILENAME_H

But I want just FILENAME_H without the PROJECTNAME_ prefix. How to change it in CLion settings?

like image 700
Kroll Avatar asked Apr 21 '16 11:04

Kroll


People also ask

How do I change my style in CLion?

Go to Settings/Preferences | Editor | Code Style, select your programming language, and open the Wrapping and Braces tab. In the Keep when reformatting section, select the formatting rules which you want to ignore and deselect those which should be applied. Reformat your code ( Ctrl+Alt+L ).

What is a header guard in C?

Header 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: #ifndef MY_HEADER_H #define MY_HEADER_H //... # endif.

Why use pragma once instead of include guard?

In the absence of #include guards around #include directives, the use of #pragma once will improve compilation speed for some compilers since it is a higher-level mechanism; the compiler itself can compare filenames or inodes without having to invoke the C preprocessor to scan the header for #ifndef and #endif .

What is an include guard in C plus plus?

In the C and C++ programming languages, an #include guard, sometimes called a macro guard, header guard or file guard, is a particular construct used to avoid the problem of double inclusion when dealing with the include directive.


2 Answers

Bit late to this question, but I've got a slightly more involved solution that'll handle this without the need for manual post-processing regardless of file extension:

  1. Head into your File and Code Templates - The other answers already detail how to do this.
  2. In the File and Code Templates settings page, change to the Includes tab.
  3. Click the + to create a new Include template. Name it something like IncludeGuard and set the Extension to h.
  4. Input the following for the contents. Make sure you don't include any blank lines before or after.
#macro( includeGuard $filename $ext )
#set( $ucfull = ${filename.toUpperCase().replace('-', '_')} )
#set( $extidx = ${ucfull.lastIndexOf(".")} )
#set( $extstart = $extidx + 1 )
#if( $extidx > -1 )
#set( $ucname = ${ucfull.substring(0,$extidx)} )
#set( $ucext = ${ucfull.substring($extstart)} )
#else
#set( $ucname = $!{ucfull} )
#set( $ucext = ${ext.toUpperCase()} )
#end
${ucname}_${ucext}##
#end##
  1. Change back to the Files tab, and find the C Header File or C++ Class Header file depending on which language you're looking to update.
  2. Change the contents of this file template to:
#parse("IncludeGuard.h")##
#set( $blank = "" )
#[[#ifndef]]# #includeGuard(${NAME} "h")${blank}
#[[#define]]# #includeGuard(${NAME} "h")${blank}

// ...

#[[#endif]]# // #includeGuard(${NAME} "h")

If everything works as intended, attempting to create a C Header File using the name test-include-guard or test-include-guard.h should both result in the following:

#ifndef TEST_INCLUDE_GUARD_H
#define TEST_INCLUDE_GUARD_H

// ...

#endif /* TEST_INCLUDE_GUARD_H */

Few notes:

  • If you need to change the file extension, change the includeGuard(${NAME} "h") parts to use whatever extension you want for the second parameter. The template will attempt to parse the file extension from ${NAME}, but ${NAME} only contains the file extension if you explicitly enter it into the new filename dialog.
  • The current state of whitespace handling in the Velocity templates used by CLion is a shit show, so you'll need to work around this as I did if you decide to further customize the templates. General guidelines:
    • If you're experiencing undesired linebreaks, you'll want to try adding a terminating line comment ## to the ends of lines before it.
    • If you find yourself in the oppostie scenario, (missing an expected linebreak) you can work around this with the #set( $blank = "" ) strategy I utilized above.
  • Most of the IntelliJ-based IDEs seem to cache the compilation of Include templates the first time they get passed into #parse(). If you make changes to an Include template after this point, you'll usually need to use the File > Invalidate Caches/Restart menu command before the changes propagate.
like image 147
Charles Grunwald Avatar answered Oct 01 '22 03:10

Charles Grunwald


  1. Settings->Editor->File and Code Templates->Files
  2. change ${INCLUDE_GUARD} into _${NAME}_H_

For example, if your file name is: clion.h, then _${NAME}_H_ is rendered as _clion_H_, because ${NAME} is rendered as the filename (without extension).

like image 31
chenyi Avatar answered Oct 01 '22 03:10

chenyi