ImportC and druntime's importc.h and __builtins.di
Walter Bright
newshound2 at digitalmars.com
Sun Sep 3 08:55:13 UTC 2023
Lots of people are trying out ImportC, which is good news! Lots of people
encounter a problem here and there with a strange error message emanating from
the compiler when compiling a .h file.
A good way to find out what is causing this error message is to run the C
preprocessor (such as cpp) over the .c file that #include's it, and direct the
result into a .i file.
A bit more work, and usually the offending line in the .i file is isolated.
A large number of these errors result from the .h file making use of some
bizzaro C language extension, that nobody who makes these .h files seems to be
able to resist.
It is not practical for ImportC to implement every C compiler's nutburger
extension. But usually there is a way to write a C macro to provide a workaround.
This is the job of druntime/src/importc.h, which is automatically #include'd by
ImportC:
https://github.com/dlang/dmd/blob/master/druntime/src/importc.h
To fix the problem you've encountered, adding a macro to importc.h can get your
code moving again (and please file a bugzilla or a PR so everyone can take
advantage of your findings!).
Another file used to compensate for nutburger C compiler extensions is
__builtins.di:
https://github.com/dlang/dmd/blob/master/druntime/src/__builtins.di
which, having access to D's constructions, can often fake a C compiler
extension. __builtins.di is also automatically imported by every compilation of
an ImportC C file.
More information about the Digitalmars-d
mailing list