<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=US-ASCII">
<META NAME="Generator" CONTENT="MS Exchange Server version 5.5.2653.12">
<TITLE>RE: [gambit-list] Linking to C++ problems</TITLE>
</HEAD>
<BODY>

<P><FONT SIZE=2>Hi Marc,</FONT>
</P>

<P><FONT SIZE=2>I've done more investigation in the DLL problem and I've tried a lot of things, different compilation switches but I cannot make it work.</FONT></P>

<P><FONT SIZE=2>I would really appreciate any suggestion you might have.</FONT>
</P>

<P><FONT SIZE=2>It goes like this:</FONT>
</P>

<P><FONT SIZE=2>Using the g++ (mingw) compiler I compiled both the program and Gambit runtime.</FONT>
<BR><FONT SIZE=2>If I replace the Scheme call with the equivalent C implementation (without going through the Gambit runtime) in all scenarios (console or graphical interface) works fine when loading the DLL.</FONT></P>

<P><FONT SIZE=2>If I call a Scheme method in a small console program it works fine. I assume that happens because the DLL is loaded at a lower memory address and the pointers in the scheme method are valid.</FONT></P>

<P><FONT SIZE=2>If however I use a graphical interface test program, the DLL gets loaded at a higher memory location and I get "Segmentation fault" inside the scheme code. It seems that some pointers inside Gambit runtime are invalid but I don't know why this happens. </FONT></P>

<P><FONT SIZE=2>This is the error:</FONT>
</P>

<P><FONT SIZE=2>Program received signal SIGSEGV, Segmentation fault.</FONT>
<BR><FONT SIZE=2>0x6abc123a in pluginEntry () from c:\compile\fox-1.4.2\tests\test.dll</FONT>
<BR><FONT SIZE=2>(gdb) bt</FONT>
<BR><FONT SIZE=2>#0  0x6abc123a in pluginEntry () from c:\compile\fox-1.4.2\tests\test.dll</FONT>
<BR><FONT SIZE=2>#1  0x6abc11b5 in entryPoint@4 () from c:\compile\fox-1.4.2\tests\test.dll</FONT>
<BR><FONT SIZE=2>#2  0x004013fc in main ()</FONT>
</P>

<P><FONT SIZE=2>And this is the code I use:</FONT>
</P>

<P><FONT SIZE=2>extern void pluginEntry(EXNOTE*);</FONT>
</P>

<P><FONT SIZE=2>#define ___VERSION 40062</FONT>
<BR><FONT SIZE=2>#include "gambit.h"</FONT>
</P>

<P><FONT SIZE=2>#define SCHEME_LIBRARY_LINKER ____20_testscm__</FONT>
</P>

<P><FONT SIZE=2>___BEGIN_C_LINKAGE</FONT>
<BR><FONT SIZE=2>extern ___mod_or_lnk SCHEME_LIBRARY_LINKER (___global_state_struct*);</FONT>
<BR><FONT SIZE=2>___END_C_LINKAGE</FONT>
</P>

<P><FONT SIZE=2>CALLBACK void  entryPoint(EXNOTE* note1)</FONT>
<BR><FONT SIZE=2>{</FONT>
</P>

<P><FONT SIZE=2>  ___setup_params_struct setup_params;</FONT>
</P>

<P><FONT SIZE=2>  ___setup_params_reset (&setup_params);</FONT>
</P>

<P><FONT SIZE=2>  setup_params.version = ___VERSION;</FONT>
<BR><FONT SIZE=2>  setup_params.linker  = SCHEME_LIBRARY_LINKER;</FONT>
</P>

<P><FONT SIZE=2>  ___setup (&setup_params);     </FONT>
<BR>        
<BR><FONT SIZE=2>  pluginEntry(note1);</FONT>
<BR><FONT SIZE=2>  </FONT>
</P>

<P><FONT SIZE=2>   ___cleanup ();</FONT>
<BR><FONT SIZE=2>}</FONT>
</P>

<P><FONT SIZE=2>(c-define-type EXNOTE* (pointer "EXNOTE"))</FONT>
<BR><FONT SIZE=2>(c-define (pluginEntry x) (EXNOTE*) void "pluginEntry" "" </FONT>
<BR><FONT SIZE=2>          (write x))</FONT>
</P>
<BR>

<P><FONT SIZE=2>Thank you,</FONT>
<BR><FONT SIZE=2>Dan.</FONT>
</P>
<BR>

<P><FONT SIZE=2>-----Original Message-----</FONT>
<BR><FONT SIZE=2>From: Marc Feeley [<A HREF="mailto:feeley@IRO.UMontreal.CA">mailto:feeley@IRO.UMontreal.CA</A>] </FONT>
<BR><FONT SIZE=2>Sent: Monday, February 21, 2005 5:55 PM</FONT>
<BR><FONT SIZE=2>To: dhristodorescu@borderfree.com</FONT>
<BR><FONT SIZE=2>Cc: gambit-list@IRO.UMontreal.CA</FONT>
<BR><FONT SIZE=2>Subject: Re: [gambit-list] Linking to C++ problems</FONT>
</P>

<P><FONT SIZE=2>> Sorry to replay to my own message but I figure out what was the =</FONT>
<BR><FONT SIZE=2>> problem.</FONT>
<BR><FONT SIZE=2>> It was just the mangling of DLL exported symbols (I was referring a =</FONT>
<BR><FONT SIZE=2>> wrong</FONT>
<BR><FONT SIZE=2>> name); it works fine now.</FONT>
</P>

<P><FONT SIZE=2>Great.</FONT>
</P>

<P><FONT SIZE=2>> The only relevant part of the message that remains is the one about the</FONT>
<BR><FONT SIZE=2>> extern "C" generation.</FONT>
</P>

<P><FONT SIZE=2>I don't understand your point here.  You should either compile</FONT>
<BR><FONT SIZE=2>the whole Gambit runtime system and your program with a C</FONT>
<BR><FONT SIZE=2>compiler, or with a C++ compiler, and then the names will be</FONT>
<BR><FONT SIZE=2>mangled appropriately.  Are you mixing C and C++ code?  (this is</FONT>
<BR><FONT SIZE=2>a bad idea in general because of the way exceptions are handled....)</FONT>
</P>

<P><FONT SIZE=2>Marc</FONT>
</P>

</BODY>
</HTML>