ImportC and #pragma pack
Walter Bright
newshound2 at digitalmars.com
Tue Sep 28 23:54:52 UTC 2021
On 9/28/2021 12:52 PM, Guillaume Piolat wrote:
> On Tuesday, 28 September 2021 at 17:32:26 UTC, Walter Bright wrote:
>> On 9/28/2021 5:50 AM, Guillaume Piolat wrote:
>>> The conundrum with ImportC is that it seems to me you have to build with the
>>> right cstdlib headers if you intend to use this or that backend.
>>
>> Not sure what that means.
>
> I mean that the ImportC preprocessor needs libc headers, and those SHOULD be the
> same libc that the code will be linked against.
Just like for any C code, the .h files must match the library binaries.
> 1. Suppose you use ImportC right now, using the TinyCC with its own cstlib
> headers, for the purpose of preprocessing C code in order to give to ImportC.
>
> The preprocessed C code has the following `dirent` struct:
> https://github.com/TinyCC/tinycc/blob/82b0af74501bf46b16bc2a4a9bd54239aa7b7127/win32/include/dirent.h#L25
>
>
> 2. You link with the libc that comes with DMD or LDC, so the clang or msvc or
> Digital Mars one. They contains another `dirent` struct definition
>
> 3. At runtime, the C stdlib has its own understanding of what is the `dirent`
> struct. A memory corruption ensues right after calling readdir() and reading the
> result.
Of course that will fail, just like it will fail if you compile code with
TinyCC's stdlib and link with clang's stdlib.
More information about the Digitalmars-d
mailing list