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?
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 ).
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.
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 .
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.
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:
+
to create a new Include template. Name it something like IncludeGuard
and set the Extension to h
.#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##
C Header File
or C++ Class Header
file depending on which language you're looking to update.#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:
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.##
to the ends of lines before it.#set( $blank = "" )
strategy I utilized above.#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.${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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With