www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Calling D libraries from C, _deh_beg madness and exception handling

reply Nicolay Korslund <korslund gmail.com> writes:
Hi, I'm having some problems making a D library and linking it to a C/C++
program. (The library is Monster, http://dsource.org/projects/monster.) I want
the library to be as accessible to C++ users as possible, so I want to be able
to distribute it as a static library and let users access the library though C
function calls. I specifically do NOT want to force users to download a D
compiler in order to use the library.

I have created an init() function that initializes the GC and the likes (copied
from internal/dmain2), but problems occur when linking - I get undefined
references to _deh_beg and _deh_end. Googling around I found that this is a
known problem (DMD inserts these symbols when it finds a main() function to set
up exception handling), but I couldn't find a solution. I found that defining

void* _deh_beg;
void* _deh_end;

in the C program makes the problem go away, but of course exceptions become
horribly broken.

Is there any way to make DMD insert the correct symbols without having a D
main? Or can I set them up myself somehow? I don't even know what these
pointers are supposed to point to.

N
Oct 29 2007
parent reply Nicolay Korslund <korslund gmail.com> writes:
Nicolay Korslund Wrote:
 
 Is there any way to make DMD insert the correct symbols without having a D
main? Or can I set them up myself somehow? I don't even know what these
pointers are supposed to point to.
 
Apparantly the solution was to add an empty main() function in D code somewhere. For some reason this did not create a conflict with the C main(), so now everything is peace and harmony. Let's just hope it doesn't suddenly stop working again at some point. N
Oct 29 2007
next sibling parent reply BCS <BCS pathlink.com> writes:
Nicolay Korslund wrote:
 Nicolay Korslund Wrote:
 
Is there any way to make DMD insert the correct symbols without having a D
main? Or can I set them up myself somehow? I don't even know what these
pointers are supposed to point to.
Apparantly the solution was to add an empty main() function in D code somewhere. For some reason this did not create a conflict with the C main(), so now everything is peace and harmony. Let's just hope it doesn't suddenly stop working again at some point. N
IIRC there is an extern(C)main defined in phobos that calls the D main. because of the way that linkers work, the main in phobos gets cover up by just about anything that should get precedence. So while I'd still call you code a hack/workaround, I'd say it should keep working.
Oct 29 2007
parent Nicolay Korslund <korslund gmail.com> writes:
BCS Wrote:

 IIRC there is an extern(C)main defined in phobos that calls the D main. 
 because of the way that linkers work, the main in phobos gets cover up 
 by just about anything that should get precedence. So while I'd still 
 call you code a hack/workaround, I'd say it should keep working.
Ok, thanks, that's what I suspected. I think this should documented somewhere, since writing libraries is a pretty important feature and I'm not the first person to ask about it. The closest thing I could find on the website is a Windows DLL guide. I might write a cross-language library guide myself, but I have to play some more with it myself though.
Oct 30 2007
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Nicolay Korslund" <korslund gmail.com> wrote in message 
news:fg5nqc$27rn$1 digitalmars.com...
 Nicolay Korslund Wrote:
 Is there any way to make DMD insert the correct symbols without having a 
 D main? Or can I set them up myself somehow? I don't even know what these 
 pointers are supposed to point to.
Apparantly the solution was to add an empty main() function in D code somewhere. For some reason this did not create a conflict with the C main(), so now everything is peace and harmony. Let's just hope it doesn't suddenly stop working again at some point. N
The D main that you write gets converted to _Dmain, thus the symbols don't collide.
Oct 29 2007