Kro*_*oll 12 c++ include-guards clion
当CLion创建一个头文件时,它会添加包含这样的保护字符串:
#ifndef PROJECTNAME_FILENAME_H
#define PROJECTNAME_FILENAME_H
/* ... code ... */
#endif //PROJECTNAME_FILENAME_H
Run Code Online (Sandbox Code Playgroud)
但我想要FILENAME_H没有PROJECTNAME_前缀.如何在CLION设置中更改它?
小智 7
${INCLUDE_GUARD}成_${NAME}_H_例如,如果您的文件名是:clion.h,则_${NAME}_H_呈现为_clion_H_,因为${NAME}呈现为文件名(没有扩展名).
这个问题有点晚了,但是我有一个稍微复杂的解决方案,可以解决此问题,而无需手动进行后处理,而无需考虑文件扩展名:
+以创建一个新的包含模板。将其命名为IncludeGuard,并将Extension设置为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##
Run Code Online (Sandbox Code Playgroud)
C Header File或C++ Class Header文件。#parse("IncludeGuard.h")##
#set( $blank = "" )
#[[#ifndef]]# #includeGuard(${NAME} "h")${blank}
#[[#define]]# #includeGuard(${NAME} "h")${blank}
// ...
#[[#endif]]# // #includeGuard(${NAME} "h")
Run Code Online (Sandbox Code Playgroud)
如果一切正常,请尝试使用该名称创建C头文件,test-include-guard否则test-include-guard.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##
Run Code Online (Sandbox Code Playgroud)
一些注意事项:
includeGuard(${NAME} "h")部件以将所需的任何扩展名用作第二个参数。该模板将尝试从中解析文件扩展名${NAME},但${NAME}仅在您将文件扩展名明确输入新文件名对话框时才包含该文件扩展名。##在其前面的行尾添加终止行注释。#set( $blank = "" )我上面使用的策略来解决此问题。#parse()。如果您在此之后对“包含”模板进行了更改,通常需要在更改传播之前使用“ 文件” >“ 无效的缓存/重新启动”菜单命令。