www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17975] New: D produces mangling incompatible with C++

https://issues.dlang.org/show_bug.cgi?id=17975

          Issue ID: 17975
           Summary: D produces mangling incompatible with C++
           Product: D
           Version: D2
          Hardware: x86
                OS: Mac OS X
            Status: NEW
          Severity: critical
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: edwards.ac gmail.com

// 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

--
Nov 08 2017