www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - writing pure C program with dmd / linking error

reply Matthias Walter <walter mail.math.uni-magdeburg.de> writes:
I'm trying to write a pure C program with D, like:

extern (C) {

int main(int argc, char **argv) {
        return 1;
}

}

I did:
dmd -c test.d
gcc -o test test.o

the linker complains: 
test.o: In function `gcc2_compiled.':
test.d:(.text+0x8): undefined reference to `_Dmodule_ref'

I need this, as I want to write a library, which contains C and D, so it must
be linkable with a simple c object file. Of course, in this case, I could have
simply written it as a c file, but my library seams to need it, too. The symbol
is in libphobos.a, but my library should not include / link against phobos.

Is it possible, to turn off generation of this reference? Is it very important
for things to work?

Or is it even impossible to create libraries with C and D? 

What about writing C programs the way I did to benefit from all those compiler
features like version syntax, DDoc, contract programming, etc.

I'm running Linux on x86 with dmd-1.009 and gcc-4.1.1
Mar 11 2007
next sibling parent Sean Kelly <sean f4.ca> writes:
Matthias Walter wrote:
 I'm trying to write a pure C program with D, like:
 
 extern (C) {
 
 int main(int argc, char **argv) {
         return 1;
 }
 
 }
 
 I did:
 dmd -c test.d
 gcc -o test test.o
 
 the linker complains: 
 test.o: In function `gcc2_compiled.':
 test.d:(.text+0x8): undefined reference to `_Dmodule_ref'
I think any D app at least requires a valid D entry point: int main(char[][] args ); (no extern (C)) The rest can all be C, though, if you'd like.
Mar 11 2007
prev sibling parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
Matthias Walter wrote:
 I'm trying to write a pure C program with D, like:
 
 extern (C) {
 
 int main(int argc, char **argv) {
         return 1;
 }
 
 }
 
 I did:
 dmd -c test.d
 gcc -o test test.o
 
 the linker complains: 
 test.o: In function `gcc2_compiled.':
 test.d:(.text+0x8): undefined reference to `_Dmodule_ref'
 
 I need this, as I want to write a library, which contains C and D, so it must
be linkable with a simple c object file. Of course, in this case, I could have
simply written it as a c file, but my library seams to need it, too. The symbol
is in libphobos.a, but my library should not include / link against phobos.
 
 Is it possible, to turn off generation of this reference? Is it very important
for things to work?
 
 Or is it even impossible to create libraries with C and D? 
 
 What about writing C programs the way I did to benefit from all those compiler
features like version syntax, DDoc, contract programming, etc.
 
 I'm running Linux on x86 with dmd-1.009 and gcc-4.1.1
There is no way to stop the compiler inserting the reference to phobos. Even if you *could* get it to leave it out, and you managed to not compile in Phobos, you would be unable to use: exceptions, module constructors, unit tests, new/delete, arrays, or any of the basic types where it needs the TypeInfo. So it's probably not feasible to leave it out. It would be kind of like wanting to use Java without the Java VM. Basically, D is not simply a C compiler with extra "compiler features" added on: it's a wholly separate language. If you want to write a "pure C" program, then you should probably write it in C. That said, it might be possible to make libraries that can be used by C programs using gdc under linux -- I don't use either, so I won't comment one way or the other on that. Apologies for the terse response, I just woke up and haven't hads me coffee yet... -- Daniel "ugh; damnit sun, stop coming up so early!" -- Unlike Knuth, I have neither proven or tried the above; it may not even make sense. v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP http://hackerkey.com/
Mar 11 2007
parent Matthias Walter <walter mail.math.uni-magdeburg.de> writes:
Daniel Keep Wrote:

 
 
 Matthias Walter wrote:
 I'm trying to write a pure C program with D, like:
 
 extern (C) {
 
 int main(int argc, char **argv) {
         return 1;
 }
 
 }
 
 I did:
 dmd -c test.d
 gcc -o test test.o
 
 the linker complains: 
 test.o: In function `gcc2_compiled.':
 test.d:(.text+0x8): undefined reference to `_Dmodule_ref'
 
 I need this, as I want to write a library, which contains C and D, so it must
be linkable with a simple c object file. Of course, in this case, I could have
simply written it as a c file, but my library seams to need it, too. The symbol
is in libphobos.a, but my library should not include / link against phobos.
 
 Is it possible, to turn off generation of this reference? Is it very important
for things to work?
 
 Or is it even impossible to create libraries with C and D? 
 
 What about writing C programs the way I did to benefit from all those compiler
features like version syntax, DDoc, contract programming, etc.
 
 I'm running Linux on x86 with dmd-1.009 and gcc-4.1.1
There is no way to stop the compiler inserting the reference to phobos. Even if you *could* get it to leave it out, and you managed to not compile in Phobos, you would be unable to use: exceptions, module constructors, unit tests, new/delete, arrays, or any of the basic types where it needs the TypeInfo. So it's probably not feasible to leave it out. It would be kind of like wanting to use Java without the Java VM. Basically, D is not simply a C compiler with extra "compiler features" added on: it's a wholly separate language. If you want to write a "pure C" program, then you should probably write it in C. That said, it might be possible to make libraries that can be used by C programs using gdc under linux -- I don't use either, so I won't comment one way or the other on that. Apologies for the terse response, I just woke up and haven't hads me coffee yet...
Thanks for this response, so I'm gonna split it up into a C library, compiled with a C compiler and a D library, which uses the C one, compiled with DMD/gdc. Matthias Walter
Mar 12 2007