Jump to page: 1 2
Thread overview
How to make D resolve C++ symbols by mangling symbols with the Itanium ABI on Windows
Feb 26
thumbgun
Feb 26
thumbgun
Feb 26
user1234
Feb 26
thumbgun
Feb 26
user1234
Feb 26
user1234
Feb 26
thumbgun
Feb 26
user1234
Feb 26
user1234
Feb 27
thumbgun
Feb 26
Johan
Feb 27
thumbgun
Mar 01
thumbgun
Mar 01
thumbgun
February 26

I'm currently trying to call some C++ functions that were compiled by g++ (mingw). However g++ uses the Itanium ABI name mangling rules. dmd on Windows tries to link functions based on the MSVC name mangling rules. For example:

// add.cpp
int add(int lhs, int rhs)
{
    return lhs + rhs;
}


// main.d
import std.stdio;

extern(C++) int add(int lhs, int rhs);

void main()
{
    auto x = add(5, 6);
    writeln(x);
}


// Command line
g++ add.cpp -c -o add.obj
dmd main.d add.obj


// Output
main.obj : error LNK2019: unresolved external symbol "int __cdecl add(int,int)"
(?add@@YAHHH@Z) referenced in function _Dmain

The prefix for MSVC symbols is '?' while the Itanium ABI's prefix is '_Z'. Is there any way I can make dmd link to symbols mangled according to the Itanium ABI's rules on Windows?

February 26

The title wasn't very accurate as it isn't dmd that mangles the names but my question still stands.

February 26

On Monday, 26 February 2024 at 13:40:56 UTC, thumbgun wrote:

>

The title wasn't very accurate as it isn't dmd that mangles the names but my question still stands.

Have you tried pragma mangle ?

February 26

On Monday, 26 February 2024 at 13:58:46 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 13:40:56 UTC, thumbgun wrote:

>

The title wasn't very accurate as it isn't dmd that mangles the names but my question still stands.

Have you tried pragma mangle ?

I have seen it but it's not really what I need. If I don't find a solution I might have to build all of the C++ libraries I need with MSVC.

February 26

On Monday, 26 February 2024 at 14:41:26 UTC, thumbgun wrote:

>

On Monday, 26 February 2024 at 13:58:46 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 13:40:56 UTC, thumbgun wrote:

>

The title wasn't very accurate as it isn't dmd that mangles the names but my question still stands.

Have you tried pragma mangle ?

I have seen it but it's not really what I need.
[...]
Not even

pragma(mangle, "?add@@YAHHH@Z")
extern(C++) int add(int lhs, int rhs);

?

February 26

On Monday, 26 February 2024 at 15:46:34 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 14:41:26 UTC, thumbgun wrote:

>

On Monday, 26 February 2024 at 13:58:46 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 13:40:56 UTC, thumbgun wrote:

>

The title wasn't very accurate as it isn't dmd that mangles the names but my question still stands.

Have you tried pragma mangle ?

I have seen it but it's not really what I need.
[...]
Not even

pragma(mangle, "?add@@YAHHH@Z")
extern(C++) int add(int lhs, int rhs);

?

Sorry, the one starting with a qmark is already what the D obj thinks it has to be linked to. I meant

pragma(mangle, "_Zadd@@YAHHH@Z")
extern(C++) int add(int lhs, int rhs);
February 26

On Monday, 26 February 2024 at 15:49:40 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 15:46:34 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 14:41:26 UTC, thumbgun wrote:

>

On Monday, 26 February 2024 at 13:58:46 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 13:40:56 UTC, thumbgun wrote:

>

The title wasn't very accurate as it isn't dmd that mangles the names but my question still stands.

Have you tried pragma mangle ?

I have seen it but it's not really what I need.
[...]
Not even

pragma(mangle, "?add@@YAHHH@Z")
extern(C++) int add(int lhs, int rhs);

?

Sorry, the one starting with a qmark is already what the D obj thinks it has to be linked to. I meant

pragma(mangle, "_Zadd@@YAHHH@Z")
extern(C++) int add(int lhs, int rhs);

The name mangling differences between the Itanium ABI and the MSVC ABI are more significant than just the prefix. I only mentioned that the prefixes are different to show that dmd was actually trying to link against symbols mangled by the MSVC ABI, not the Itanium one. If I used pragma mangle then I would have to manually translate each C++ function (signature) and struct/class to what the Itanium ABI would mangle them to which is very error prone and time consuming. I'm going to read the dmd source code - and maybe make a fork - because it apparently only tries to link against MSVC mangled symbols on Windows and not other platforms.

February 26

On Monday, 26 February 2024 at 17:23:32 UTC, thumbgun wrote:

>

[...]
The name mangling differences between the Itanium ABI and the MSVC ABI are more significant than just the prefix. I only mentioned that the prefixes are different to show that dmd was actually trying to link against symbols mangled by the MSVC ABI, not the Itanium one. If I used pragma mangle then I would have to manually translate each C++ function (signature) and struct/class to what the Itanium ABI would mangle them to which is very error prone and time consuming. I'm going to read the dmd source code - and maybe make a fork - because it apparently only tries to link against MSVC mangled symbols on Windows and not other platforms.

I see. That would be extremely painful. Could CTFE help here ?

string getRightMangling(alias T)()
{
    return "todo" // ;)
}

pragma(mangle, getRightMangling!(add))
extern(C++) int add(int lhs, int rhs);

That's not a serious suggestion but I'm still curious to see how that would scale.

Also that looks weird that the pragma refers to what it is supposed to affect, not sure that would work, although in theory D manages forward refs.

February 26

On Monday, 26 February 2024 at 13:36:42 UTC, thumbgun wrote:

>

I'm currently trying to call some C++ functions that were compiled by g++ (mingw). However g++ uses the Itanium ABI name mangling rules. dmd on Windows tries to link functions based on the MSVC name mangling rules.
...
Is there any way I can make dmd link to symbols mangled according to the Itanium ABI's rules on Windows?

Rather than the mangling names of functions, I'd be more worried about calling conventions (something that mismatched name mangling is supposed to help you with! ;-)).

You can try with LDC and -mtriple=x86_64-windows-gnu.

-Johan

February 26

On Monday, 26 February 2024 at 19:50:31 UTC, user1234 wrote:

>

On Monday, 26 February 2024 at 17:23:32 UTC, thumbgun wrote:

>

[...]

I see. That would be extremely painful. Could CTFE help here ?

string getRightMangling(alias T)()
{
    return "todo" // ;)
}

pragma(mangle, getRightMangling!(add))
extern(C++) int add(int lhs, int rhs);

That's not a serious suggestion but I'm still curious to see how that would scale.

Also that looks weird that the pragma refers to what it is supposed to affect, not sure that would work, although in theory D manages forward refs.

Turns out that the forward ref is not a problem. Remains the question of writing a mangler working fast at compile time ;)

« First   ‹ Prev
1 2