C++/C mangleof inconsistency for OS X

Dan Olson via Digitalmars-d digitalmars-d at puremagic.com
Tue Apr 21 10:01:51 PDT 2015


Dan Olson <zans.is.for.cans at yahoo.com> writes:

> Jacob Carlborg <doob at me.com> writes:
>
>> On 2015-04-20 18:33, Dan Olson wrote:
>>> An observation on OSX w/ 2.067: mangleof for C++ (and D) names produces
>>> the actual object file symbol while mangleof for C names strips a
>>> leading underscore.
>>>
>>> Is this intended?  If so what is rationale?
>>
>> I don't think it's intentional. The point of "mangleof" is to evaluate
>> to the actual mangled name, as it appears in the object file.
>
> Thanks Jacob.
>
> In that case, mangleof for extern(C) names on OS X and other systems
> that add a leading underscore should include the underscore.
>
> extern(C) int x;
> version(linux) pragma(msg, foo.mangleof); // "x"
> version(OSX) pragma(msg, foo.mangleof);   // "_x"
>
> I'm trying to understand because ldc is different than dmd, and it is
> related to proper debugging on systems with leading underscores.
> pragma(mangle, name) is wrapped up in this too.  This needs to be right
> to help D expand to other systems.

Hmmm, I can see another point of view where mangleof should produce the
equivalent extern(C) symbol.  My gut says this is the way it should
work.

If I want to call a C function void debug(const char*) from a C library,
I would do this because of D "debug" keyword:

  pragma(mangle, "debug")
  extern (C) void debug_c(const(char*));

Now I would think debug_c.mangleof -> "debug"
(and that is indeed what dmd produces even on OS X).

On systems which prepend an underscore, we want compiler to take care of
this so code is portable, otherwise code must do this:

version (OSX)
  pragma(mangle, "_debug") extern (C) void debug_c(const(char*));
else
  pragma(mangle, "debug") extern (C) void debug_c(const(char*));

--
Dan


More information about the Digitalmars-d mailing list