如何自定义eclipse CDT代码模板

发布于 2024-10-24 21:02:53 字数 441 浏览 1 评论 0原文

我需要为项目编写的代码符合某些风格指南。然而,CDT 中包含的标准模板与这种风格不匹配。尤其是头护罩的布局不应该是这样的。我看了一下模板,对于我的 Eclipse,它看起来像这样:

${filecomment}

#ifndef ${include_guard_symbol}
#define ${include_guard_symbol}

${typecomment}
${declarations}

#endif /* ${include_guard_symbol} */

所以我猜测变量 ${include_guard_symbol} 是在 CDT 中的某处设置的,但是是否可以在不需要的情况下更改此设置修改CDT本身?

略有不同但相关的注释: 是否可以添加您自己的模板,以便您可以使用项目的常规新对话框添加其他类型的新文件(测试用例、专用类等)?

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 the header guards is not the way it should be. I had a look at the template and for my Eclipse it looks like this:

${filecomment}

#ifndef ${include_guard_symbol}
#define ${include_guard_symbol}

${typecomment}
${declarations}

#endif /* ${include_guard_symbol} */

So I am guessing the variable ${include_guard_symbol} is set somewhere in the CDT, but is it possible to change this setting without needing to modify the CDT itself?

On a slightly different, but related note:
Is it possible to add your own templates, so you just could add new files of other types (test-cases, specialized classes etc) using the normal new dialog for the project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅笑轻吟梦一曲 2024-10-31 21:02:53

我们的项目也遇到过类似的困难。一种解决方案是在模板中一起丢弃 ${include_guard_symbol} ,并自己定义它,可能使用一些其他预定义变量。像这样的东西:

${filecomment}

#ifndef MyProject_${file_base}_h
#define MyProject_${file_base}_h

${typecomment}
${declarations}

#endif /* MyProject_${file_base}_h */

因此,对于名为 inc/Foo.h 的头文件,包含保护将像这样插入:

#ifndef MyProject_Foo_h
#define MyProject_Foo_h

不幸的是,似乎没有一种方法可以自定义更多内容。例如,如果我定义了一个嵌套在命名空间中的类,我可能希望将命名空间作为包含保护的一部分。目前我找不到在 eclipse 中做到这一点的方法。

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.

擦肩而过的背影 2024-10-31 21:02:53

因此,在 C/C++ 下的“首选项”对话框中 ->代码风格->代码模板您可以修改模板以更接近您需要的内容,例如,如果您需要守卫中的名称空间,您可以执行类似的操作。

${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} */

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} */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文