Using C macros without massive rewrites
nospam at example.com
Tue Jun 12 00:51:41 PDT 2012
Am Tue, 12 Jun 2012 06:15:38 +0200
schrieb Alex Rønne Petersen <alex at lycus.org>:
> On 12-06-2012 04:20, Charles McAnany wrote:
> > Hi, all. I'm studying Kerrisk's The Linux Programming Interface for
> > fun. The book is written in C, and I thought it would be fun to do
> > the exercises in D. My problem is that I'm doing things like
> > #include <sys/types.h>
> > in my C code and that loads oodles of macros like ssize_t, O_RDONLY,
> > EXIT_SUCCESS, etc.
> > I don't mind writing .di files for the occasional function I use,
> > but tracking down every last typedef and #define is going to be a
> > hassle. The functions aren't bad because you can just make a C file
> > that #includes all the libraries you want, cc it to lib.o then
> > write a .di that uses the functions you want from that mammoth.
> > Extracting the #defines and typedefs from stdlib.h (and everything
> > it includes) sounds, uh, daunting. (this suspicion is corroborated
> > by Deimos, which has no commits to the libc repository)
> > I was thinking it wouldn't be too bad if I could write a .c file
> > that somehow conveniently boxes up the relevant information for
> > a .di file to extract it, but I'm at a loss for where to go with
> > this.
> > Alternatively, I could maybe run the .di file through gcc for
> > preprocessing only and hope that I don't confuse its lexer.
> > Has anyone here had any experience with these things?
> The core.stdc.* package has modules for all C99 headers and for most
> system headers of each platform (Linux, OS X, FreeBSD, Windows, etc).
> And in general, there's no good way to automatically translate macros
> to D.
But for simple macros, gcc -E -dM can be useful.
gcc -E -dM '/usr/include/sys/types.h'
#define __DBL_MIN_EXP__ (-1021)
#define __UINT_LEAST16_MAX__ 65535
#define __FLT_MIN__ 1.17549435082228750797e-38F
More information about the Digitalmars-d-learn