writing pure C program with dmd / linking error

Matthias Walter walter at mail.math.uni-magdeburg.de
Mon Mar 12 05:03:25 PDT 2007


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


More information about the Digitalmars-d-learn mailing list