Hello I am having the following problem. I am trying to turn a program I have written into a shared object. I have ran into some problems however. When I use writeln instead of printf my program segfaults. I have edited the code to just the parts causing the problem.

=================main.d the so========================

import std.stdio;
import core.stdc.stdio;

extern (C) void main(){
        writeln("Does it work?");
        printf("Works");
}

==============main.c the main executable======================

#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>

int main() {
  printf("+main()\n");

  void *lh = dlopen("THE PATH EDITED FOR MY SECURITY", RTLD_LAZY);
  if (!lh) {
    fprintf(stderr, "dlopen error: %s\n", dlerror());
    exit(1);
  }
  printf("libdll.so is loaded\n");

  void (*fn)() = dlsym(lh, "main");
  char *error = dlerror();
  if (error) {
    fprintf(stderr, "dlsym error: %s\n", error);
    exit(1);
  }
  printf("dll() function is found\n");
  (*fn)();

  printf("unloading libdll.so\n");
  dlclose(lh);

  printf("-main()\n");
  return 0;
}

========================The compile script===============

dmd -c main.d -fPIC

dmd -oflibdll.so main.o -shared -defaultlib=libphobos2.so -L-rpath=/usr/lib/x86_64-linux-gnu -L-ldl -gc

gcc -c main.c
gcc -rdynamic main.o -o main -ldl

=====================================================================
When I have just printf is works. When I have writeln it segfaults.

This fails

import std.stdio;

extern (C) void main(){
        writeln("Does it work?");
}

This works

import core.stdc.stdio;

extern (C) void main(){
        printf("Works");
}

===========================================================================

Any one know whats up? Thanks!
-Harpo


Reply via email to