ImportC can now automatically run the preprocessor

Ali Çehreli acehreli at yahoo.com
Sun May 15 17:31:18 UTC 2022


On 5/15/22 08:34, Andrea Fontana wrote:
 > On Sunday, 15 May 2022 at 13:13:09 UTC, Adam D Ruppe wrote:

 >> What did you use it for?
 >
 > I've imported some libraries directly into D. Not the headers, the whole
 > library.


Same here.

I covered ImportC during a recent meetup. I literally did the following:


1) Copy/paste example code from libplot documentation into a D file.


2) Added the following line to my file:

   import plot;

Note: For that to work, I added the following line into my Makefile:

plot.i: plot.c Makefile
	gcc -I /usr/include -E $< > $@

Once plot.i is generated by the external preprocessor, D could just 
import it. I must disclaim that plot.c was needed to do the following:

// This is needed because __restrict is not supported
#define __restrict restrict

// This is needed to skip the definition of the __REDIRECT macro because 
certain
// compatibility declarations of 'fscanf' and friends could not be 
parsed. (?)
#undef __GNUC__

// This is needed because plot.h uses FILE but fails to include stdio.h
#include "stdio.h"

// This is what our program needs
#include "plot.h"


3) I changed two places in the example C code:

3.i) Added .dup.ptr to the following literal because D's literals are 
not the same as C's literals:

   "PAGESIZE".dup.ptr

3.ii) I changed the following manifest constant to D:

// #define MAXORDER = 12;
enum MAXORDER = 12;


Summary: I (almost) copy/pasted C code into D and the entire code worked 
without even thinking about any binding.

Then I further experimenting with importing C files and D files between 
each other. It just worked.

I understand it is even simpler today because I think the issue with 
__restrict is gone and D can preprocess.

Ali



More information about the Digitalmars-d mailing list