www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [gdc] c and d linking issue

reply Michael <mhamptonNO SPAMmacmail.com> writes:
I'm trying to compile this code on OS X Panther with gdc:

[ctest.c]
#include <stdio.h>

extern void callMe();

int main() {
	callMe();
}

[callme.d]
class Foo {
    this() {
    }
}

extern (C) void callMe() {
    Foo s = new Foo();		// This causes the problem.
}

I'm compiling and linking like this:
gcc -c ctest.c
gdc -c callme.d
gdc -o ctest callme.o ctest.o

Running ctest causes a 'Bus error'.  After playing with it for a while, 
instantiating Foo seems to be the problem.  If I comment out that line 
it compiles and runs just fine.  Is this behavior expected?  Am I doing 
something wrong or is this a bug?

I'm using gdc 1f with gcc 3.4.0 20040317 on Mac OS 10.3.3.

P.S.  I wasn't sure if this should have gone in the bugs group, since I 
don't know if it affects dmd or not.  I also am not sure if this is 
just a mistake on my part or a real bug.  If I did put this in the 
wrong group, I apologize and please let me know for future reference.  
Thanks.
May 18 2004
parent reply David Friedman <d3rdclsmail earthlink.net> writes:
I think the problem is this: The program is starting from the C main in 
ctest.c and garbage collector is not being initialized.

The Phobos library contains a C 'main' function which does 
initialization and calls the D 'main' function (really '_Dmain').

Do you absolutely need the main function to be in C?

David

Michael wrote:
 I'm trying to compile this code on OS X Panther with gdc:
 
 [ctest.c]
 #include <stdio.h>
 
 extern void callMe();
 
 int main() {
     callMe();
 }
 
 [callme.d]
 class Foo {
    this() {
    }
 }
 
 extern (C) void callMe() {
    Foo s = new Foo();        // This causes the problem.
 }
 
 I'm compiling and linking like this:
 gcc -c ctest.c
 gdc -c callme.d
 gdc -o ctest callme.o ctest.o
 
 Running ctest causes a 'Bus error'.  After playing with it for a while, 
 instantiating Foo seems to be the problem.  If I comment out that line 
 it compiles and runs just fine.  Is this behavior expected?  Am I doing 
 something wrong or is this a bug?
 
 I'm using gdc 1f with gcc 3.4.0 20040317 on Mac OS 10.3.3.
 
 P.S.  I wasn't sure if this should have gone in the bugs group, since I 
 don't know if it affects dmd or not.  I also am not sure if this is just 
 a mistake on my part or a real bug.  If I did put this in the wrong 
 group, I apologize and please let me know for future reference.  Thanks.
 
May 19 2004
parent reply Michael <mhamptonNO SPAMmacmail.com> writes:
Thanks, I figured that may be the problem.  I'm actually wondering 
because SDL on OS X uses an objective C stub to initialize the program 
then call your SDL_main function.  I have seen at least one SDL program 
written in D running on OS X(but without source), so there must be a 
way.  Do I have to use all global functions?  Things seem to work if I 
avoid classes, but I would prefer not to use that approach as it seems 
likely I might run into other problems down the road.  Are there any 
work arounds?  Thanks.

On 2004-05-19 10:23:48 -0500, David Friedman <d3rdclsmail earthlink.net> said:

 I think the problem is this: The program is starting from the C main in 
 ctest.c and garbage collector is not being initialized.
 
 The Phobos library contains a C 'main' function which does 
 initialization and calls the D 'main' function (really '_Dmain').
 
 Do you absolutely need the main function to be in C?
 
 David
 
 Michael wrote:
 I'm trying to compile this code on OS X Panther with gdc:
 
 [ctest.c]
 #include <stdio.h>
 
 extern void callMe();
 
 int main() {
     callMe();
 }
 
 [callme.d]
 class Foo {
    this() {
    }
 }
 
 extern (C) void callMe() {
    Foo s = new Foo();        // This causes the problem.
 }
 
 I'm compiling and linking like this:
 gcc -c ctest.c
 gdc -c callme.d
 gdc -o ctest callme.o ctest.o
 
 Running ctest causes a 'Bus error'.  After playing with it for a while, 
 instantiating Foo seems to be the problem.  If I comment out that line 
 it compiles and runs just fine.  Is this behavior expected?  Am I doing 
 something wrong or is this a bug?
 
 I'm using gdc 1f with gcc 3.4.0 20040317 on Mac OS 10.3.3.
 
 P.S.  I wasn't sure if this should have gone in the bugs group, since I 
 don't know if it affects dmd or not.  I also am not sure if this is 
 just a mistake on my part or a real bug.  If I did put this in the 
 wrong group, I apologize and please let me know for future reference.  
 Thanks.
May 19 2004
parent reply David Friedman <d3rdclsmail earthlink.net> writes:
I ran into this problem too.  What I do now is rename the 'main' in the 
SDL stub.  Because each project has its own copy of the stub as a source 
file, this is very clean.  The call sequence becomes...

normal D main
   -> renamed SDL stub main
     -> SDL_main (written as "extern(C) SDL_main(int,char*) {....}" in D)

My old solution was to build another version Phobos that uses 
'_d_rtl_start' as an entry point instead of 'main'.  I then had the 
SDL_main call that.  The gdc Makefile for Phobos still has this 
libphobosnm.a, but needs shinichiro's fix.
(http://www.digitalmars.com/drn-bin/wwwnews?D.gnu/626)

Neither or these is optimal.  I have been thinking that Phobos 
initialization and the D 'main' function should be separated.  This 
would be needed for shared libraries too.

David

Michael wrote:
 Thanks, I figured that may be the problem.  I'm actually wondering 
 because SDL on OS X uses an objective C stub to initialize the program 
 then call your SDL_main function.  I have seen at least one SDL program 
 written in D running on OS X(but without source), so there must be a 
 way.  Do I have to use all global functions?  Things seem to work if I 
 avoid classes, but I would prefer not to use that approach as it seems 
 likely I might run into other problems down the road.  Are there any 
 work arounds?  Thanks.
 
 On 2004-05-19 10:23:48 -0500, David Friedman <d3rdclsmail earthlink.net> 
 said:
 
 I think the problem is this: The program is starting from the C main 
 in ctest.c and garbage collector is not being initialized.

 The Phobos library contains a C 'main' function which does 
 initialization and calls the D 'main' function (really '_Dmain').

 Do you absolutely need the main function to be in C?

 David

 Michael wrote:

 I'm trying to compile this code on OS X Panther with gdc:

 [ctest.c]
 #include <stdio.h>

 extern void callMe();

 int main() {
     callMe();
 }

 [callme.d]
 class Foo {
    this() {
    }
 }

 extern (C) void callMe() {
    Foo s = new Foo();        // This causes the problem.
 }

 I'm compiling and linking like this:
 gcc -c ctest.c
 gdc -c callme.d
 gdc -o ctest callme.o ctest.o

 Running ctest causes a 'Bus error'.  After playing with it for a 
 while, instantiating Foo seems to be the problem.  If I comment out 
 that line it compiles and runs just fine.  Is this behavior 
 expected?  Am I doing something wrong or is this a bug?

 I'm using gdc 1f with gcc 3.4.0 20040317 on Mac OS 10.3.3.

 P.S.  I wasn't sure if this should have gone in the bugs group, since 
 I don't know if it affects dmd or not.  I also am not sure if this is 
 just a mistake on my part or a real bug.  If I did put this in the 
 wrong group, I apologize and please let me know for future 
 reference.  Thanks.
May 19 2004
parent Michael Hampton <mhamptonNO SPAMmacmail.com> writes:
Thank you.  That worked perfectly.  I really appreciate you taking the 
time to respond and the work you put into gdc.

In article <c8gdn5$2gm$1 digitaldaemon.com>,
 David Friedman <d3rdclsmail earthlink.net> wrote:

 I ran into this problem too.  What I do now is rename the 'main' in the 
 SDL stub.  Because each project has its own copy of the stub as a source 
 file, this is very clean.  The call sequence becomes...
 
 normal D main
    -> renamed SDL stub main
      -> SDL_main (written as "extern(C) SDL_main(int,char*) {....}" in D)
 
 My old solution was to build another version Phobos that uses 
 '_d_rtl_start' as an entry point instead of 'main'.  I then had the 
 SDL_main call that.  The gdc Makefile for Phobos still has this 
 libphobosnm.a, but needs shinichiro's fix.
 (http://www.digitalmars.com/drn-bin/wwwnews?D.gnu/626)
 
 Neither or these is optimal.  I have been thinking that Phobos 
 initialization and the D 'main' function should be separated.  This 
 would be needed for shared libraries too.
 
 David
 
 Michael wrote:
 Thanks, I figured that may be the problem.  I'm actually wondering 
 because SDL on OS X uses an objective C stub to initialize the program 
 then call your SDL_main function.  I have seen at least one SDL program 
 written in D running on OS X(but without source), so there must be a 
 way.  Do I have to use all global functions?  Things seem to work if I 
 avoid classes, but I would prefer not to use that approach as it seems 
 likely I might run into other problems down the road.  Are there any 
 work arounds?  Thanks.
 
 On 2004-05-19 10:23:48 -0500, David Friedman <d3rdclsmail earthlink.net> 
 said:
 
 I think the problem is this: The program is starting from the C main 
 in ctest.c and garbage collector is not being initialized.

 The Phobos library contains a C 'main' function which does 
 initialization and calls the D 'main' function (really '_Dmain').

 Do you absolutely need the main function to be in C?

 David

 Michael wrote:

 I'm trying to compile this code on OS X Panther with gdc:

 [ctest.c]
 #include <stdio.h>

 extern void callMe();

 int main() {
     callMe();
 }

 [callme.d]
 class Foo {
    this() {
    }
 }

 extern (C) void callMe() {
    Foo s = new Foo();        // This causes the problem.
 }

 I'm compiling and linking like this:
 gcc -c ctest.c
 gdc -c callme.d
 gdc -o ctest callme.o ctest.o

 Running ctest causes a 'Bus error'.  After playing with it for a 
 while, instantiating Foo seems to be the problem.  If I comment out 
 that line it compiles and runs just fine.  Is this behavior 
 expected?  Am I doing something wrong or is this a bug?

 I'm using gdc 1f with gcc 3.4.0 20040317 on Mac OS 10.3.3.

 P.S.  I wasn't sure if this should have gone in the bugs group, since 
 I don't know if it affects dmd or not.  I also am not sure if this is 
 just a mistake on my part or a real bug.  If I did put this in the 
 wrong group, I apologize and please let me know for future 
 reference.  Thanks.
May 19 2004