D Class in a DLL

Raphael Basso avatarRaphael Basso created an issue

Hello. I am trying to create a DLL using the GDC (gcc-4.6.1-tdm64-1-gdc-5aa95e2678-20120613-D2.058), and I am using the following code:

// testdll.d

module testdll;

import std.c.windows.windows;

__gshared HINSTANCE g_hInst;

extern(C) int _tls_callbacks_a;

version(Windows)
{
	extern(Windows) BOOL DllMain(HINSTANCE hInstance, ULONG ulReason, LPVOID pvReserved)
	{
		import core.sys.windows.dll;

		switch (ulReason)
		{
			case DLL_PROCESS_ATTACH:
				dll_process_attach(hInstance, true);
			break;
			case DLL_PROCESS_DETACH:
				dll_process_detach(hInstance, true);
			break;
			case DLL_THREAD_ATTACH:
				dll_thread_attach(true, true);
			break;
			case DLL_THREAD_DETACH:
				dll_thread_detach(true, true);
			return false;
			default:
			break;
		}

		g_hInst = hInstance;

		return true;
	}
}

export int sum(int x, int y)
{
	return x + y;
}

And

// main.d

import std.stdio;
import testdll;

int main(string [] args)
{
	writefln("%d", sum(10, 20));

	return 0;
}

To compile, I'm using:

gdc -shared -o testdll.dll testdll.d -Wl,--out-implib,libtestdll.a
gdc -fintfc -v2 -fsyntax-only -H testdll.d
gdc -o main.exe main.d -L. -ltestdll

This code compiles without errors and runs normally. The problem is when I put a class within the DLL and try to compile the code:

import std.stdio;

export class Test
{
	public void hello()
	{
		writefln("Hello!");
	}
}

The DLL compiles normally, the problem is when I try to link the library with the program:

undefined reference to `_D7testdll4Test7__ClassZ'
undefined reference to `_D7testdll12__ModuleInfoZ'

There is something missing in my code, or the GDC has not implemented full support for DLLs?

Comments (7)

  1. Daniel Green

    Attempting to recreate the issue, I only receive the following error.

    C:\MinGW\msys\1.0\home\venix\mingw-gdc.git\issues\352/main.d:9: undefined reference to `_D7testdll4Test7__ClassZ'
    

    The ModuleInfoZ issue was discovered and corrected back in January due to how DMD exports certain symbols in comparison to GDC. Likely ClassZ falls under this issue as well. Based on that, there will probably be an issue with interfaces, struct, and any other Z symbol that requires linking with a DLL.

    I'm going to take another look at how this is done in GDC to determine if there's a fix that can hit all issues at once, or if it becomes necessary to declare Z symbol as export.

    I know that Dll's and classes can work with MinGW/GDC. However, I don't think anybody is actively using them in their projects and nobody is working on improving that area.

    The biggest issue I discovered was related to exceptions not being handled properly across Dll boundaries which is also an issue with GCC C++ due to how Dll's load copies of the core types.

  2. Raphael Basso

    Thanks for responding so quickly.

    My intention in creating DLLs in D is precisely to extend the language because there are very few libraries (and number of users), which makes me "upset" because the power of language, and the number of possibilities it offers. I think that's the biggest barrier that prevents new developers to use.

    I have several projects in C++ that would like to porting them to the D language, as my 3D Game Engine (which is divided into modules, each module is a DLL / SO), my ToolKit (based on the WinAPI and Xlib), among other software and libraries.

    Another point to note about the lack of support for Shared Libraries in D, is that the executables are extremely large. It would be very advantageous to create a library runtime for the language, to prevent huge programs.

    Have not given up the D language (as well as the GDC), and eagerly await the day that I can develop with "complete freedom" in this environment ...

  3. Daniel Green

    You can use the the following when linking your Dll to output all symbols.

    -Wl,--export-all-symbols

    This may work for you until I'm able to release an update that exports the correct symbols.

    A side effect of this is that you can only link against the Dll due to duplicate symbols if you generate the .a file.

  4. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.