Elegant way to use dynamic bindings

Dennis dkorpel at gmail.com
Fri Feb 9 15:31:01 UTC 2018


On Friday, 9 February 2018 at 14:51:30 UTC, Mike Parker wrote:
> Where was the lib file located? Was it in the root project 
> directory? How are you compiling your project? What does your 
> directory tree look like?
> (...)
> That depends. Is that the directory where your executable is 
> written? Are you launching it from inside an Visual Studio or 
> from the command line? Normally, the executable's directory is 
> on the system path, but when using VS the default configuration 
> is to write the executables into configuration-specific 
> subdirectories.

I use `dub run` from cmd or Mono-D. All .exe, .lib and .dll files 
are in the root. Source files are in the subdirectory 'source'.

I tried to reduce the problem by making a simple test folder 
containing the necessary .dll files, import library and source 
files:

freetype6.dll
libpng12.dll
libpng3.dll
main.d
main.exe
main.obj
mupen64plus-audio-sdl.dll
mupen64plus-input-sdl.dll
mupen64plus-rsp-hle.dll
mupen64plus-video-glide64mk2.dll
mupen64plus-video-rice.dll
mupen64plus.dll
mupen64plus.lib
SDL.dll
zlib1.dll

Where main.d is:
```
import std.stdio;

version(dynamiclink) {

	pragma(lib, "mupen64plus.lib");
	extern(C) nothrow @nogc {
		int CoreGetAPIVersions(int*, int*, int*, int*);
	}
	
	int main() {
		int a, b, c, d;
		CoreGetAPIVersions(&a, &b, &c, &d);
		writeln("Version: ", a, " - ", b, " - ", c, " - ", d);
		return 0;
	}
}

version(dynamicload) {

	import core.sys.windows.windows: LoadLibrary, GetProcAddress, 
GetLastError;
	extern(C) alias p_CoreGetAPIVersions = int function(int*, int*, 
int*, int*);
	p_CoreGetAPIVersions CoreGetAPIVersions;

	int main() {
		auto handle = LoadLibrary("mupen64plus.dll");
		CoreGetAPIVersions = cast(p_CoreGetAPIVersions) 
GetProcAddress(handle, "CoreGetAPIVersions");
		
		int a, b, c, d;
		CoreGetAPIVersions(&a, &b, &c, &d);
		writeln("Version: ", a, " - ", b, " - ", c, " - ", d);
		return 0;
	}
}
```

With `dmd main.d -version=dynamicload` it works, but with `dmd 
main.d -version=dynamiclink` it still gives this:

main.obj(main)
Error 42: Symbol Undefined _CoreGetAPIVersions
Error: linker exited with status 1


More information about the Digitalmars-d-learn mailing list