www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 17359] New: C++ Interfacing: function with 'static' array


          Issue ID: 17359
           Summary: C++ Interfacing: function with 'static' array
                    parameter cannot be linked (x64)
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ParticlePeter gmx.de

C++ Interfacing: function with 'static' array parameter cannot be linked (x64)

C++ prototype:
bool cppFunc( float[3] color );

D binding (as described here [1])
extern(C++) bool cppFunc( ref float color );

Using with:
float[3] my_color;
cppFunc( my_color );

Building with dmd
-> error LNK2001: unresolved external symbol "bool __cdecl cppFunc(float
(&)[3])" Binding.exe : fatal error LNK1120: 1 unresolved externals
Error: linker exited with status 1120
dmd failed with exit code 1120.

Same problem but slightly different error messages with LDC. 

The issue is discussed here in additional detail [2].

Ali mentions that following variant works for him on linux:
// deneme.cpp
float cppFunc(float color[3]) {
    return color[0] + color[1] + color[2];

$ g++ -c deneme.cpp -o deneme_cpp.o

// deneme.d
extern(C++) float cppFunc(float * color);

void main() {
    float[3] my_color = [ 1.5, 2.5, 3.5 ] ;
    assert(cppFunc(my_color.ptr) == 7.5);

$ dmd deneme_cpp.o deneme.d -of=deneme
This varinat leads to the same compiler error (with "bool __cdecl
cppFunc(float*)" message) on windows compiled for x64.

kinke points out:
Microsoft's C++ compiler doesn't mangle `float arg[3]` parameters identically
to `float* arg`:

void cppSArray(float color[3]) => ?cppSArray  YAXQEAM Z
void cppPtr(float* color) => ?cppPtr  YAXPEAM Z

This contradicts Alis variant but not the 'ref' variant. But I belief that the
'ref' variant is also a mangling problem.

[1] http://dlang.org/spec/interfaceToC.html#passing_d_array
[2] https://forum.dlang.org/post/qcmrdrebuuhjfendyzud forum.dlang.org

Apr 28