Backgrounder:
A Delphi .pas source code compiled with -JPNH option generates .hpp file
for inclusion into a C++ source file.
When the Delphi compiler is set for some alignments, quad word alignment
in particular, then filler bytes are padded between class member
variables as needed to enforce the variable's data type alignment. The
filler bytes, of cousre, are NOT visible in the generated .hpp file.
Consequently, when the .hpp file is included in a C++ source file, the
C++ compiler cannot, without direction, provide equivalent padding for
references to Delphi-side class member references.
Consequently, the class member offsets may vary between the two object
files, those generated from the Delphi compiler, and those generated
from the C++ compiler.
Case in point: TIdMessage.cpp where FDate TDateTime, being a double,
gets 4 bytes of nothing (padding) inserted before it in order to
establish alignment, whereas, the IdMessage.hpp header, included into a
project being compiled with double word alignment will not have the
4-byte gap inserted, thereby causing the object generation of TIdMessage
class instances which have the members' offset's differing from those
for which the code to act on those class instances was generated. In
short, class member offset differences between app and lib will lead to
app crashes.
Is there, or is there not, a technique to prevent this scenario from
occurring? If so, then how.
Add one of the following lines:
(*$HPPEMIT '#pragma option push -a8' *)
(*$HPPEMIT '#pragma option push -a4' *)
(*$HPPEMIT '#pragma option push -a2' *)
(*$HPPEMIT '#pragma option push -a1' *)
near the top of the file so that the .hpp file generated from the Delphi
compile step will contain one of the corresponding following:
#pragma option push -a8
#pragma option push -a4
#pragma option push -a2
#pragma option push -a1
Naturally, you will need to set the Delphi compiler |Project | Options |
Advanced Compiler | Data alignment to match the emitted alignment:
Quadword, put -a8 in the HPPEMIT line
Doubleword, put -a4
Word -a2
Byte -a1
C++ Kylix3 apparently can only generate a .hpp file by a command line
activation of the compiler dcc. Why? because the IDE doesn't have an
option to specify the required -JPNH option which causes the .hpp file to be
emitted as the .pas file is compiled.
k