15

我想要通过使用 Eclipse/CDT 创建一个新的 C++ 类来自动生成包含保护,但我找不到任何方法来更改${include_guard_symbol}属性。

我的愿望是一个包含如下命名空间前缀的包含守卫:

#ifndef NAMSPACE1_NAMESPACE2_HEADER_HPP

但如果我使用#ifndef ${namespace_name}_${include_guard_symbol}它,它将产生:

namepace1::namespace2::_HEADER_HPP

我怎样才能做到这一点?

4

2 回答 2

17

I had a dig around in the source for CDT, and found an undocumented preference setting you can use to change what is generated by ${include_guard_symbol}. There's no GUI for it either, but if you add the codetemplates.includeGuardGenerationScheme setting to <projectpath>/.settings/org.eclipse.cdt.ui.prefs, you can choose between file name (the default), file path or UUID.

Given the file <projectpath>/src/include/Class.h, the following values give these results:

  • 0 gives an upper-case filename, i.e. CLASS_H_
  • 1 gives a UUID, for example. HC9ABE718_D04E_411C_B5A2_F9FE1D9F9409
  • 2 gives an upper-case file path, that is, SRC_INCLUDE_CLASS_H_

To avoid any doubt, here's the contents of our .settings/org.eclipse.cdt.ui.prefs:

codetemplates.includeGuardGenerationScheme=2
eclipse.preferences.version=1
formatter_settings_version=1

It's obviously not exactly what you're after, but we use 2 to give us an approximation of our namespaces since, generally speaking, our namespaces follow our folder structure.

The relevant code is in these files in the CDT source:

  • core/org.eclipse.cdt.ui/src/org/eclipse/cdt/ui/PreferenceConstants.java for the constants for each option
  • core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/codemanipulation/StubUtility.java for the generateIncludeGuardSymbol() method that does the work.

It would be really nice to see an extra option added for using the namespace, and a GUI too.

于 2012-03-03T11:54:48.503 回答
1

我正在使用 Eclipse Oxygen (CDT 9.3),正如 Eelke 在他们的评论中描述的那样,现在已经有一段时间的 UI 设置了。

但是,它只允许您从预设方案中进行选择,没有可用的命名空间或更丰富的自定义选项。

在首选项对话框中搜索“守卫”,或导航到 C/C++ > 代码样式 > 名称样式并选择代码 > 包含守卫,然后从可用的守卫方案中进行选择。

在此处输入图像描述

于 2017-09-19T10:34:18.157 回答