## digitalmars.D.learn - Using C/C++-DLL in D

• S0urc3C0de (24/24) Aug 26 2013 Hello guys,
• Adam D. Ruppe (5/5) Aug 26 2013 When you run implib, did you use the /s option? implib /s either
• S0urc3C0de (11/16) Aug 26 2013 Hmm nope I did not but tried it now: same error.
• Andrej Mitrovic (16/19) Aug 26 2013 I don't think you'll be able to match the name mangling, GCC might use
• S0urc3C0de (8/10) Aug 26 2013 Okay, thank you, using C and compiling with gcc now works, but
"S0urc3C0de" <s0urc3c0de d.learn> writes:
Hello guys,

I'm trying to use a C++-DLL in D.
I compiled it via GCC, created a .lib-File with implib, but dmd
keeps telling me that the symbol was undefined.
For test purposes, I created a Test-DLL with just one simple
function that should output a simple text to the console.

#define TEST_DLL __declspec(dllexport)

#include <iostream>

void TEST_DLL printCpp()
{
std::cout << "Hello from Cpp-DLL :)" << std::endl;
}

I compiled everything and then tried to call this function from
inside D.

pragma(lib,"C:\\Path\\to\\Dll\\Test.lib");

extern(C++) void printCpp();

printCpp();

Everything works fine until I try to call the function. dmd says:
Error 42: Symbol Undefined _printCpp

The problem is more or less obvious but I'm not able to solve it.
By the way: this error also occurs when I'm creating the
.lib-File with a self-written .def-File.
I've searched for hours but didn't find anything that could help
me solving this, so I hope you guys can help :)

Aug 26 2013
"Adam D. Ruppe" <destructionator gmail.com> writes:
When you run implib, did you use the /s option? implib /s either
remember which)... so if you didn't use it, try adding that and
see what happens, and if you did, see what happens if you remove
that option.

Aug 26 2013
"S0urc3C0de" <s0urc3c0de d.learn> writes:
On Monday, 26 August 2013 at 22:17:07 UTC, Adam D. Ruppe wrote:
When you run implib, did you use the /s option? implib /s
don't remember which)... so if you didn't use it, try adding
that and see what happens, and if you did, see what happens if
you remove that option.

Hmm nope I did not but tried it now: same error.
By the way: I should correct the error message from above. The
correct error from dmd is:
Error 42: Symbol Undefined ?printCpp  YAXXZ (void cdecl
printCpp(void ))

The error message from above occurs when I write extern(C)
instead of extern(C++) but due to the fact that it is C++-DLL,
extern(C++) should be correct, shouldn't it?

Maybe, someone here already wrote a program that uses a C/C++-Lib
and may tell me what he exactly did?

Aug 26 2013
Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 8/27/13, S0urc3C0de <s0urc3c0de d.learn> wrote:
Hello guys,

I'm trying to use a C++-DLL in D.
I compiled it via GCC, created a .lib-File with implib

I don't think you'll be able to match the name mangling, GCC might use
one convention, DMD another.

There was a pragma(mangle) feature introduced in git-head recently,
but it seems to be screwed up because if you set the calling
convention it will end up double-mangling the function. For example:

extern(C++) pragma(mangle, "_Z8printCppv") void printCpp();

This will still not link. The following will link, but it is not correct:

pragma(mangle, "_Z8printCppv") void printCpp();

The function is missing the calling convention, so you can't call it
without causing memory corruption.

I don't understand why this mangle feature was implemented this way.
The mangle pragma should be the last say on what the mangled name of a
convention.

Anyway, the safest bet is to use C functions to interface with C++.

Aug 26 2013
"S0urc3C0de" <s0urc3c0de d.learn> writes:
On Monday, 26 August 2013 at 23:33:52 UTC, Andrej Mitrovic wrote:
Anyway, the safest bet is to use C functions to interface with
C++.

Okay, thank you, using C and compiling with gcc now works, but
isn't there really any way to directly use a C++-Lib? Cause
wrapping everything seems to be a hell of a lot work...

Actually, I was trying to use ffmpeg and isn't this a C-Lib?
Because when I tried to call a simple function
(av_register_all()), I got the same error as described above
though only with the leading underscore in the error message.

Aug 26 2013
Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 8/27/13, S0urc3C0de <s0urc3c0de d.learn> wrote:
Actually, I was trying to use ffmpeg and isn't this a C-Lib?

Seems so.

Because when I tried to call a simple function
(av_register_all()), I got the same error as described above
though only with the leading underscore in the error message.

Try calling implib with the /s switch.

Aug 26 2013
"S0urc3C0de" <s0urc3c0de d.learn> writes:
On Tuesday, 27 August 2013 at 00:14:47 UTC, Andrej Mitrovic wrote:
Try calling implib with the /s switch.

Okay thanks, works now.

Hopefully, there will be a safe way to use C++-Libs soon...

Aug 26 2013