奇怪的 Eclipse C++ #定义行为

发布于 2024-07-29 04:22:55 字数 536 浏览 9 评论 0原文

(过度依赖 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 技术交流群。

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

发布评论

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

评论(7

眸中客 2024-08-05 04:22:55

ISO C++ 规范中没有类似的内容。 最有可能的是,PRIVATE1(以及PRIVATE)在项目的其他地方定义。 请注意,这不需要是 .h 文件中的 #define - 它也可以通过 makefile 中的编译器开关进行定义。 我建议对项目目录进行完整的 grep 操作。

There's nothing like this in ISO C++ spec. Most likely, PRIVATE1 (as well as PRIVATE) 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.

挽手叙旧 2024-08-05 04:22:55

如果 PRIVATE1 解析为 PRIVATE,并且 PRIVATE 解析为 static,则 PRIVATE1 解析为到静态

If PRIVATE1 resolves to PRIVATE, and PRIVATE resolves to static, then PRIVATE1 resolves to static.

暮凉 2024-08-05 04:22:55

它们不太可能(但并非不可能)是由 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.

§对你不离不弃 2024-08-05 04:22:55

Eclipse C++ 管理的项目有点愚蠢!

如果一个项目被声明为 C++,它仍然基于文件扩展名进行构建,因此 .h 文件被预处理为 C 而不是 C++ 标头,该标头从另一个类似包装的头文件中引入 #define PRIVATE1:

#ifdef __cpluplus.

然后该项目由 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:

#ifdef __cpluplus.

The project is then linked by g++.

你的他你的她 2024-08-05 04:22:55

最好的选择是查看预处理器输出。 您没有发布您正在使用的编译器,但是如果您检查文档,大多数都可以选择“预处理到文件”,这将创建一个替换所有宏的文件。 这也许可以帮助您弄清楚发生了什么。 在 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.

半岛未凉 2024-08-05 04:22:55

在 C++ 中,它很可能被定义为“private:”。

Most likely in C++ it's defined to, well, "private:".

南街九尾狐 2024-08-05 04:22:55

你能用 PRIVATE1 声明一个函数吗? 如果是这样,只需编写一个像这样的函数:

PRIVATE1 void Foo() {
    // __FUNCSIG__ in Visual Studio, not sure about GCC
    std::cout << __FUNCSIG__ << std::endl;
}

Can you declare a function with PRIVATE1? If so, just write a function like so:

PRIVATE1 void Foo() {
    // __FUNCSIG__ in Visual Studio, not sure about GCC
    std::cout << __FUNCSIG__ << std::endl;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文