How to customize eclipse CDT code templates

前端 未结 2 1582
执念已碎
执念已碎 2021-01-18 07:55

I need the code I am writing for a project to match some style guidelines. However the standard templates included with CDT don\'t match this style. Especially the layout of

相关标签:
2条回答
  • 2021-01-18 08:30

    So in the Preferences dialog under C/C++ -> Code Style -> Code Templates you can modify the template to be closer to what you need, for example if you need the namespace in the guard, you can do something like.

    ${filecomment}
    
    #ifndef ${namespace_name}_${include_guard_symbol}
    #define ${namespace_name}_${include_guard_symbol}
    
    ${includes}
    
    ${namespace_begin}
    
    ${declarations}
    
    ${namespace_end}
    
    #endif /* ${namespace_name}_${include_guard_symbol} */
    
    0 讨论(0)
  • 2021-01-18 08:35

    We've had a similar struggle on our project. One solution is to throw out ${include_guard_symbol} in the template all together, and define it yourself, possibly using some of the other predefined variables. Something like this:

    ${filecomment}
    
    #ifndef MyProject_${file_base}_h
    #define MyProject_${file_base}_h
    
    ${typecomment}
    ${declarations}
    
    #endif /* MyProject_${file_base}_h */
    

    So for a header file named inc/Foo.h, the include guard would be inserted like this:

    #ifndef MyProject_Foo_h
    #define MyProject_Foo_h
    

    Unfortunately, there doesn't seem to be a way to customize much beyond that. For example, if I defined a class nested in a namespace, I might want to put the namespace as part of the include guard. I can't find a way to do that in eclipse, currently.

    0 讨论(0)
提交回复
热议问题