gcc linkage problem with C & D

Charles D Hixson charleshixsn at earthlink.net
Fri Jul 27 00:47:29 PDT 2007


Frits van Bommel wrote:
> Charles D Hixson wrote:
>> /usr/local/dmd/lib/libphobos.a(dmain2.o): In function `main':
>> internal/dmain2.d:(.gnu.linkonce.tmain+0x74): undefined reference to 
>> `_Dmain'
>> internal/dmain2.d:(.gnu.linkonce.tmain+0xc5): undefined reference to 
>> `_Dmain'
>> /usr/local/dmd/lib/libphobos.a(deh2.o): In function 
>> `_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable':
>> internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x9): 
>> undefined reference to `_deh_beg'
>> internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0xe): 
>> undefined reference to `_deh_beg'
>> internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x14): 
>> undefined reference to `_deh_end'
>> internal/deh2.d:(.gnu.linkonce.t_D4deh213__eh_finddataFPvZPS4deh213DHandlerTable+0x37): 
>> undefined reference to `_deh_end'
>> collect2: ld returned 1 exit status
>> make: *** [test0] Error 1
>>
> [snip makefile]
>>
>> (minus some wrapping that happened in posting).  The other files are 
>> unchanged:  test0c.c is a "hello, world" function, and test0d.d is a 
>> main routine that just calls the function.
> 
> I just had another look at your test0d.d file, and found your problem. 
> Your file is this:
> -----
> extern(C):
>     int    test();
> 
> void    main()
> {    test;    }
> -----
> That little ':' at the end of the first line means it'll apply extern(C) 
> to _everything that follows_, not just test(). So your main() is also 
> extern(C), which changes the name in the object file and doesn't invoke 
> DMDs special handling of the D main function (which emits _deh_* to help 
> exception handling support locate exception handler data).
> 
> So to fix your program, just delete the ':'. It compiled and ran fine on 
> my machine after that modification (besides some minor issues you 
> probably won't have; using a 32-bit gcc on my 64-bit machine and 
> correcting the dmd dir in the makefile for my machine).

THANK YOU!!  (Well, I feel stupid, but at least Makefile3 is 
now working.  Presumably I can get the others to work also.)


More information about the Digitalmars-d-learn mailing list