Skip to content

Instantly share code, notes, and snippets.

@ProjectMoon
Created April 14, 2011 21:52
Show Gist options
  • Save ProjectMoon/920648 to your computer and use it in GitHub Desktop.
Save ProjectMoon/920648 to your computer and use it in GitHub Desktop.
Files illustrating simple attempt at making a D shared library with GDC.
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
void main() {
void (*hiD)(void);
void* handle = dlopen("./libtest.so", RTLD_LAZY);
if (handle == NULL) {
printf("%s\n", dlerror());
exit(1);
}
hiD = dlsym(handle, "hiD");
if (hiD != NULL) {
hiD();
}
else {
printf("hiD is null\n");
}
dlclose(handle);
}
import std.stdio;
import std.c.linux.linux;
import std.string;
extern (C) void perror(char* s);
extern (C) void exit(int status);
extern (C) char* dlerror();
void main()
{
void* handle = dlopen("./libtest.so\0".ptr, RTLD_LAZY);
if (handle == null) {
printf("%s\n", dlerror());
exit(1);
}
void function() hiD = cast(void function()) dlsym(handle, ("hiD").ptr);
if (hiD != null) {
hiD();
}
else { writeln("hiD is null"); }
dlclose(handle);
}
#include <stdio.h>
void hiD() {
printf("hi from c\n");
}
import std.stdio;
extern (C) void hiD() {
writeln("hi from D lib");
}
@ProjectMoon
Copy link
Author

I've tried this several ways. Currently, I'm trying to dlopen() the D version of libtest.so from the C version of main:

gdc -c test.d -fPIC
gdc -o libtest.so -shared test.o
gcc main.c
./a.out

I receive the following error:
cannot allocate memory in static TLS block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment