为什么 Mingw 在编译时更改我的标识符名称?

发布于 2024-12-14 19:45:17 字数 1209 浏览 2 评论 0原文

我有一个小型 C++ 库,可以在 Linux(这是我的主要开发平台)上编译良好。现在我尝试在 Windows 机器(XP3)上使用 Mingw 构建它,但由于无法解释的错误,编译失败。 例如,假设我在类 AAA 中的名称空间 aaa 中具有以下方法,在文件 aaa.h 中声明:

void AAA::DrawText( foo z );

编译文件 aaa.cpp (当然包含方法实现)时,我收到以下错误:

D:\dev\[...]\aaa.cpp:644: error: no 'void aaa::AAA::DrawTextA( foo z )' member function declared in class 'aaa::AAA'

是的,你得到了它,没有拼写错误...编译器误读不知何故函数名称,并在函数标识符中添加了一个字母!

这绝对是我无法理解的。 Linux 上从来没有遇到过这样的问题。 mingw / Gcc 如何更改标识符?

名字篡改?不,这是编译后发生的。

当然,我检查了确切的命令行(我使用 IDE):没有任何问题:

mingw32-g++.exe -W  -O3 -Wall    -I..\include -I"C:\program files\OpenCV2.1\include\opencv"  -c D:\dev\...\aaa.cpp -o ..\obj\Release\src\aaa.o

但是:如果我将函数重命名为 DrawTxt(),那么一切都会正常(但我不能这样做)。这对我来说意味着标识符已经在某处定义。 OpenCV 库? grep 树,什么也没找到...是的,我还搜索了当前的包含文件夹,没有任何结果。

我看到的另一个解决方案是在某个地方(?)有一个像这样的宏:

#define DrawText DrawTextA

在某些情况下会被激活。

所以我的问题是:

  • 在我的代码中这个奇怪的名称替换是否还有其他可能性?对我来说,宏是唯一的方法,如果标识符已经在其他地方声明,它只会抛出一个错误,编译器不能(好吧......)突然更改/替换/编辑标识符。
  • 我怎样才能找到问题所在?

I have a small C++ library, compiles fine on Linux (which is my main dev platform). Now I'm trying to build it on a windows machine (XP3) with Mingw, and compiling fails, due to an unexplainable error.
For example, say I have the following method in class AAA, in namespace aaa, declared in file aaa.h:

void AAA::DrawText( foo z );

When compiling file aaa.cpp (that holds of course the methods implementation), I get the following error:

D:\dev\[...]\aaa.cpp:644: error: no 'void aaa::AAA::DrawTextA( foo z )' member function declared in class 'aaa::AAA'

Yep, you got it, no typo there... the compiler misread somehow the functions name, and added a letter to the function identifier !!!

This is absolutely beneath my comprehension. Never had such an issue on Linux. How can mingw / Gcc change an identifier ?

Name mangling ? No, this happens after compiling.

I checked of course the exact command line (I use an IDE): nothing wrong:

mingw32-g++.exe -W  -O3 -Wall    -I..\include -I"C:\program files\OpenCV2.1\include\opencv"  -c D:\dev\...\aaa.cpp -o ..\obj\Release\src\aaa.o

BUT: if I rename the function to, say, DrawTxt(), then everything goes fine (but I can't do that). This means to me that the identifier is already defined somewhere. Opencv lib ? Grepped the tree, found nothing... And yes, I searched also the current include folder, nothing close.

The other solution that I see is that there is somewhere (?) a macro like:

#define DrawText DrawTextA

that gets activated in some situation.

So my questions are:

  • Are there other possibilities for this curious name replacement in my code ? To me, the macro is the only way, if the identifier was already declared elsewhere, it would just throw an error, a compiler can not (well...) abruply change/replace/edit an identifier.
  • How can I trackdown the problem ?

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

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

发布评论

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

评论(2

一抹微笑 2024-12-21 19:45:18

好吧,我几乎可以肯定这是因为在某个地方发生了这种情况:

#define DrawText DrawTextA

为什么?因为后缀 A 通常表示“ascii”,而后缀 W 表示“宽”(通常用于 unicode)。这是 Windows 代码中的常见做法,通过简单的定义开关来统一 ascii 和 unicode 构建。

另外,这些函数似乎存在于 Windows 库中: http://msdn.microsoft.com/en -us/library/ms901121.aspx。您的项目中是否包含 windows.h

Well, I'm almost certain it's because somewhere this happens:

#define DrawText DrawTextA

Why? Because the suffix A often means "ascii" opposed to the suffix W which means "wide" (often for unicode). This is a common practice in Windows code to unify ascii and unicode builds with a simple define switch.

Also, it seems that the functions exists in the Windows library: http://msdn.microsoft.com/en-us/library/ms901121.aspx. Is windows.h is included in your project?

〗斷ホ乔殘χμё〖 2024-12-21 19:45:18

您包含了 windows.h 并将 DrawText 宏替换为 DrawTextA。

除了避免使用 Windows 也使用的名称之外,您对此无能为力。不太吸引人。

You included windows.h and the DrawText macro replaced DrawText with DrawTextA.

There's not a whole lot you can do about that other than avoid using names that Windows also uses. Not very appealing.

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