ImportC (The GNU D Compiler)

### 2.3 Importing C Sources into D ¶

ImportC is a C preprocessor and parser embedded into the GNU D implementation. It enables direct importation of C files, without needing to manually prepare a D file corresponding to the declarations in the C file.

ImportC is an implementation of ISO/IEC 9899:2011, which will be referred to as C11. Prior versions, such as C99, C89, and K+R C, are not supported.

Assuming you have no file `cstdio.c` or `main.d`, the commands

      cat > cstdio.c << @EOC
      int printf(const char*, ...);
      @EOC
      cat > main.d << @EOD
      import cstdio;
      void main() { printf("Hello ImportC\n"); }
      @EOD
      gdc main.d -o main; ./main

will generate a program which will print ‘`Hello ImportC`’.

ImportC does not have a preprocessor. It is designed to compile C files after they have been first run through the C preprocessor. If the C file has a ‘`.i`’ extension, the file is presumed to be already preprocessed. Preprocessing can be run manually:

      gcc -E file.c > file.i

ImportC collects all the `#define` macros from the preprocessor run when it is run automatically. The macros that look like manifest constants, such as:

    #define COLOR 0x123456

are interpreted as D manifest constant declarations of the form:

    enum COLOR = 0x123456;

The variety of macros that can be interpreted as D declarations may be expanded, but will never encompass all the metaprogramming uses of C macros.

GNU D does not directly compile C files into modules that can be linked in with D code to form an executable. When given a source file with the suffix ‘`.c`’, the compiler driver program `gdc` instead runs the subprogram `cc1`.

    gdc file1.d file2.c // d21 file1.d -o file1.s
                        // cc1 file2.c -o file2.s
                        // as file1.s -o file1.o
                        // as file2.s -o file2.o
                        // ld file1.o file2.o