奇怪的 Eclipse C++ #定义行为
(过度依赖 IDE 的情况)
我有一些遗留的 C 代码,为了单元测试的目的,我将其编译为 C++。 C 源代码是 C++ 感知的,因为它根据环境有条件地定义。
例如 (PRIVATE
解析为 static
):
#if!defined __cplusplus
#define PRIVATE1 PRIVATE
#endif
...
PRIVATE1 const int some_var;
问题是我似乎无法找出 PRIVATE1
解析为或是什么在 C++ 中,如果我添加声明但没有指示位置,编译器会抱怨重新定义?
我搜索了我的 MinGW/gcc 包含路径、C++ ISO 规范和我可用的 C++ 书籍,但没有结果。
编辑:
当然,我在发布之前检查了命令行和 makefile。
(A case of over relying on an IDE)
I have some legacy C code that I compile as C++ for the purpose of Unit testing. The C source is C++ aware in that it conditionally defines based on environment.
E.g. (PRIVATE
resolves to static
):
#if!defined __cplusplus
#define PRIVATE1 PRIVATE
#endif
...
PRIVATE1 const int some_var;
The problem is I just can't seem to find out what PRIVATE1
resolves to or is in C++, the compiler complains of redefinition if I add a declaration but doesn't indicate where?
I have searched my MinGW/gcc include path, the C++ ISO specification and the C++ books available to me has been to no avail.
Edit:
Sure I checked the command line and makefiles before posting.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
ISO C++ 规范中没有类似的内容。 最有可能的是,
PRIVATE1
(以及PRIVATE
)在项目的其他地方定义。 请注意,这不需要是 .h 文件中的#define
- 它也可以通过 makefile 中的编译器开关进行定义。 我建议对项目目录进行完整的 grep 操作。There's nothing like this in ISO C++ spec. Most likely,
PRIVATE1
(as well asPRIVATE
) are defined elsewhere in the project. Note that this doesn't need to be a#define
in an .h file - it can also be defined via compiler switches in the makefile. I'd suggest doing a full grep on the project directory.如果
PRIVATE1
解析为PRIVATE
,并且PRIVATE
解析为static
,则PRIVATE1
解析为到静态
。If
PRIVATE1
resolves toPRIVATE
, andPRIVATE
resolves tostatic
, thenPRIVATE1
resolves tostatic
.它们不太可能(但并非不可能)是由 MinGW 本身定义的。 C++ 或 C 实现定义的宏应以下划线开头。
It's unlikely (but not impossible) that they are defined by MinGW itself. Macros defined by the C++ or C implementation should begin with an underscore.
Eclipse C++ 管理的项目有点愚蠢!
如果一个项目被声明为 C++,它仍然基于文件扩展名进行构建,因此 .h 文件被预处理为 C 而不是 C++ 标头,该标头从另一个类似包装的头文件中引入 #define PRIVATE1:
然后该项目由 g++ 链接。
Eclipse C++ managed project's are a little, well stupid!
If a project is declared C++ it still bases it's build on file extension, hence .h file preprocessed as C and not C++ header which pulls in a #define PRIVATE1 from another header file similarly wrapped by:
The project is then linked by g++.
最好的选择是查看预处理器输出。 您没有发布您正在使用的编译器,但是如果您检查文档,大多数都可以选择“预处理到文件”,这将创建一个替换所有宏的文件。 这也许可以帮助您弄清楚发生了什么。 在 Visual Studio 中,您使用 /E 选项(在 C/C++ -> 预处理器 -> 生成预处理文件下),它将把 foo.c 转换为 foo.i。 与原始源文件相比,该文件通常会很大,因此向下滚动到底部以查看您的代码。
Your best bet is to look at the preprocessor output. You didn't post what compiler you are using, but if you check the docs, most have an option to "Preprocess to file" which will create a file with all the macros substituted. This might be able to help you figure out what is happening. In Visual Studio you use the /E option (under C/C++->Preprocessor->Generate Preprocessed File) which will turn foo.c into foo.i. This file will generally be HUGE compared to the original source file, so scroll down to the bottom to see your code.
在 C++ 中,它很可能被定义为“private:”。
Most likely in C++ it's defined to, well, "private:".
你能用 PRIVATE1 声明一个函数吗? 如果是这样,只需编写一个像这样的函数:
Can you declare a function with PRIVATE1? If so, just write a function like so: