www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - SIGSEGV when using D DLL with Qt MinGW

reply Jerry <Kickupx gmail.com> writes:
Hello guys, as the title says I'm getting a SIGSEGV when trying 
to use a D DLL.

Let's take a look on this C++ code:


extern "C" __declspec(dllimport) void D_user_fillEngine(const 
char* workDir, void* engine);
extern "C" __declspec(dllimport) int D_user_startUp();
extern "C" __declspec(dllimport) int D_user_terminate();

int main(int argc, char *argv[])
{
     D_user_startUp();
     D_user_fillEngine("path", nullptr);
     QApplication app(argc, argv);

     QQmlApplicationEngine engine;
     engine.load(QUrl(QStringLiteral("qrc:/main.qml")));

     int res = app.exec();
     D_user_terminate();
     return res;
}


Just normal Qt startup code with some external DLL 
loading/calling by OS.
The D_user_* calls just have to be there to cause the SIGSEGV, 
not being executed. So I guess something is wrong with the way 
this DLL is loaded.

And the D code:

export extern(C) void D_user_fillEngine(char* c_workDir, void* 
c_qmlEngine) {
      //Foo
}

export extern(C) int D_user_startUp() {
      //Intended for rt_init();
}

export extern(C) int D_user_terminate() {
     //Intended for rt_term();
}


I am using the following environment:

Windows 7
Qt 5.5
MinGW 4.9
DMD 2.69.1
DUB (with dynamicLibrary option)

Everything is x86.

I am really stuck here. Thanks on beforehand.
Feb 23 2016
parent reply Mike Parker <aldacron gmail.com> writes:
On Tuesday, 23 February 2016 at 08:50:45 UTC, Jerry wrote:
 I am using the following environment:

 Windows 7
 Qt 5.5
 MinGW 4.9
 DMD 2.69.1
 DUB (with dynamicLibrary option)

 Everything is x86.

 I am really stuck here. Thanks on beforehand.
I'm surprised you're able to get an executable when linking with the import library. By default, DMD uses the OPTLINK linker for 32-bit output, which creates object files in OMF format. MinGW uses COFF. If you want 32-bit COFF output from DMD, you'll need to make sure you have the Microsoft compiler tools installed (Visual Studio Community Edition is an easy way to get everything you need) and use the -m32mscoff command line switch with DMD when compiling your DLL. However, there are often incompatibilities between MinGW's COFF and Microsoft's COFF, so you may still get no joy.
Feb 23 2016
parent Jerry <Kickupx gmail.com> writes:
On Tuesday, 23 February 2016 at 11:10:30 UTC, Mike Parker wrote:
 I'm surprised you're able to get an executable when linking 
 with the import library.
I actually just tried a bunch of extern(?), extern "?" combinations and it compiled.
 If you want 32-bit COFF output from DMD, you'll need to make 
 sure you have the Microsoft compiler tools installed (Visual 
 Studio Community Edition is an easy way to get everything you 
 need) and use the -m32mscoff command line switch with DMD when 
 compiling your DLL. However, there are often incompatibilities 
 between MinGW's COFF and Microsoft's COFF, so you may still get 
 no joy.
I guess I should switch C++ compiler then. Just to stay on the safe side. Maybe Clang. Tryed to Google for the format but didn't find it. Also it seems someone has compiled Qt using it.
Feb 23 2016