dynamic library building and loading

Rob T rob at ucora.com
Thu Sep 27 16:21:07 PDT 2012


For me to get C or C++ to run a D function, I had to do the 
following:

// ====================
// C/C++ library source
// ====================

// sample D function from library
void foo(int i, int j, int k);
// D runtime initialization & shutdown
void init();
void done();

void bar()
{
     foo(6, 7, 8);
}

int main(int argc, char **argv)
{
init();
	bar();
	done();
	return 0;
}


// ================
// D library source
// ================
import std.stdio;
static import core.runtime;

// sample D function for test
extern (C++) //	int foo(int i, int j, int k)
	void foo(int i, int j, int k)
	{
		writefln("i = %s", i);
		writefln("j = %s", j);
		writefln("k = %s", k);
		int t = i + j + k;
		writefln("Total = %s", t);
	}

// Had to initialize and shutdown D system from C/C++.
extern (C++) export void init() { // to be called once after 
loading shared lib
     core.runtime.Runtime.initialize();
}

extern (C++) export void done() { // to be called before 
unloading shared lib
     core.runtime.Runtime.terminate();
}


// I had to include main even though this is a library.
int main()
{
    return 0;
}





More information about the Digitalmars-d mailing list