www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C structs

reply "Johann Lermer" <jlermer gmx.de> writes:
Hi,

I'm fiddling around with cairo (downloaded 
fromhttps://github.com/D-Programming-Deimos/cairo) and I stumbled 
over this problem:

(file rectandmatrix.d)
----
import deimos.cairo.cairo;

void main ()
{
     cairo_rectangle_int_t rect;
     cairo_matrix_t matrix;
}
----

Compiling that with 'dmd rectandmatrix' fails with this error:

rectandmatrix.o: In function `_Dmain':
rectandmatrix.d:(.text._Dmain+0x18): undefined reference to 
`_D6deimos5cairo5cairo14cairo_matrix_t6__initZ'
collect2: error: ld returned 1 exit status
--- errorlevel 1

The rectangle is OK - no error there. The matrix however, 
produces the error. The difference between both is that 
cairo_rectangle_int_t is a struct containing integers, and 
cairo_matrix_t contains doubles.

I can, however, compile like that:

'dmd rectandmatrix deimos/cairo/cairo.d' (deimos being in a 
subdir of the working directory)

then cairo.d is compiled as well and linked to the code.

So, my questions are:

1) Why do I have to compile and link cairo.d - it's only an 
definition file for C code and IMHO there shouldn't be any need 
to compile cairo.d at all.

2) Why ist a struct containing only integers handled so 
differently from a struct holding doubles?

Thanks
Jun 20 2014
next sibling parent "Kagamin" <spam here.lot> writes:
On Friday, 20 June 2014 at 10:51:20 UTC, Johann Lermer wrote:
 `_D6deimos5cairo5cairo14cairo_matrix_t6__initZ'
it's an init value for the struct; in D all data is initialized.
Jun 20 2014
prev sibling parent reply "Dicebot" <public dicebot.lv> writes:
By D specification you are always required to compiled all 
imported modules, it does not matter if those contain only 
definitions. Some of implicitly generated symbols (like T.init) 
will be in their object files.
Jun 20 2014
parent reply "Johann Lermer" <jlermer gmx.de> writes:
Agreed, but I assumed, that since all definitions in cairo.d are 
defined as
extern (System), (same happens with extern (C), btw.), I would 
have expected,
that D does not implicitly generate initialisation functions.

So, why is there no init routine for the rectangle? There's only 
one for the matrix.
Jun 20 2014
parent reply "Dicebot" <public dicebot.lv> writes:
On Friday, 20 June 2014 at 12:17:22 UTC, Johann Lermer wrote:
 Agreed, but I assumed, that since all definitions in cairo.d 
 are defined as
 extern (System), (same happens with extern (C), btw.), I would 
 have expected,
 that D does not implicitly generate initialisation functions.
D requires that all variable are default-initialized, extern or not -> T.init is necessary.
 So, why is there no init routine for the rectangle? There's 
 only one for the matrix.
That needs actually building deimos cairo and checking symbols in object files so I will do as soon as have a bit more spare time ;)
Jun 20 2014
parent Artur Skawina via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
On 06/20/14 14:42, Dicebot via Digitalmars-d-learn wrote:
 On Friday, 20 June 2014 at 12:17:22 UTC, Johann Lermer wrote:
 So, why is there no init routine for the rectangle? There's only one for the
matrix.
That needs actually building deimos cairo and checking symbols in object files so I will do as soon as have a bit more spare time ;)
If i were to guess: the compiler optimizes the init-blitting into a memset(-equivalent) when all the members are default initted to zero; D's doubles are initialized to NaN, so this optimization is not happening when a struct contains such fields. artur
Jun 21 2014