Bug 17975 – D produces mangling incompatible with C++

Status
RESOLVED
Resolution
INVALID
Severity
critical
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Mac OS X
Creation time
2017-11-09T04:26:58Z
Last change time
2018-10-22T18:16:30Z
Keywords
C++, mangling
Assigned to
No Owner
Creator
Andrew Edwards

Comments

Comment #0 by edwards.ac — 2017-11-09T04:26:58Z
// Link to original newsgroup post: https://goo.gl/UF17xe // example.cpp ================================== import std.stdio; alias pwchar = ushort; extern (C++) int call_cpp(); // pragma(mangle, "__Z3fooPtiPKcS1_PS1_") extern (C++) int foo(pwchar* b, int bs, const char* it, const char* ite, const char** itr) { writeln("Inside: foo()"); return 0; } void main(){ call_cpp(); } ================================== // example.cpp ================================== #include "stdio.h" typedef unsigned short pwchar; int foo(pwchar* b, int bs, const char* it, const char* ite, const char** itr); extern "C++" int call_cpp() { printf("Inside: call_cpp()\n"); foo(NULL, 0, NULL, NULL, NULL); return 0; } ================================== //System info: uname -smrp Darwin 17.0.0 x86_64 i386 // Compiler info: This is the latest nightly build curl -fsS https://dlang.org/install.sh | bash -s dmd-nightly source ~/dlang/dmd-master-2017-11-01/activate dmd --version DMD64 D Compiler v2.077.0-rc.1-master-f344696 Copyright (c) 1999-2017 by Digital Mars written by Walter Bright //Steps to produce Linker error: dmd -c dexample c++ -c example.cpp dmd dexample.o example.o Undefined symbols for architecture x86_64: "foo(unsigned short*, int, char const*, char const*, char const**)", referenced from: call_cpp() in example.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) // C++ output nm example.o| grep foo U __Z3fooPtiPKcS1_PS1_ // original D output nm dexample.o | grep foo 0000000000000440 S __Z3fooPtiPKcS1_PKPS0_ 0000000000000068 S ___Z3fooPtiPKcS1_PKPS0_.eh // Output when compiled with pragma(mangle, "__Z3fooPtiPKcS1_PS1_") enabled nm dexample.o | grep foo 0000000000000440 S __Z3fooPtiPKcS1_PS1_ 0000000000000068 S ___Z3fooPtiPKcS1_PS1_.eh
Comment #1 by kinke — 2017-11-09T18:01:41Z
Well, when using a proper signature in D (transitive const!), it seems to work fine: extern (C++) int foo(pwchar* b, int bs, const(char)* it, const(char)* ite, const(char)** itr); => https://godbolt.org/g/V7eR52
Comment #2 by pro.mathias.lang — 2018-10-22T18:16:30Z
Indeed, it's a transitive const issue. We should properly document how to bind C++ to D (as not all code can be), but that's another issue. Closing as invalid.