Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Mingw change my identifier name at compile time?

Tags:

c++

mingw

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 ?
like image 601
kebs Avatar asked Dec 27 '22 11:12

kebs


2 Answers

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?

like image 129
orlp Avatar answered Jan 15 '23 03:01

orlp


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.

like image 33
David Heffernan Avatar answered Jan 15 '23 03:01

David Heffernan