www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Using C macros without massive rewrites

reply "Charles McAnany" <dlang charlesmcanany.com> writes:
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?
Jun 11 2012
parent reply =?UTF-8?B?QWxleCBSw7hubmUgUGV0ZXJzZW4=?= <alex lycus.org> writes:
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. -- Alex Rønne Petersen alex lycus.org http://lycus.org
Jun 11 2012
parent reply Johannes Pfau <nospam example.com> writes:
Am Tue, 12 Jun 2012 06:15:38 +0200
schrieb Alex R=C3=B8nne Petersen <alex 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?
=20 The core.stdc.* package has modules for all C99 headers and for most=20 system headers of each platform (Linux, OS X, FreeBSD, Windows, etc). =20 And in general, there's no good way to automatically translate macros to D. =20
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 [...]
Jun 12 2012
parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 6/12/12, Johannes Pfau <nospam example.com> wrote:
 But for simple macros, gcc -E -dM can be useful.
 gcc -E -dM '/usr/include/sys/types.h'
This could be useful for me, thanks. More options: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Preprocessor-Options.html
Jun 12 2012