How to automatically load typedef and macro definition from .h files of C language?

Hello, I am learning Julia language and trying to use some external C library.

Although it is easy to call C functions using ccall, it is quite difficult to handle the different typedef’s and macro define’s of a certain library.
For example, the following code is an example code from FFTW documentation (there has already been julia wrapper for FFTW, I’m just using this example as illustration):

#include <fftw3.h>
...
{
    fftw_complex *in, *out;
    fftw_plan p;
    ...
    in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
    p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
    ...
    fftw_execute(p); /* repeat as needed */
    ...
    fftw_destroy_plan(p);
    fftw_free(in); fftw_free(out);
}

The types fftw_complex, fftw_plan, and macros FFTW_FORWARD, FFTW_ESTIMATE are all defined in “fftw3.h”.
By reading the documentation of Julia Calling C and Fortran Code · The Julia Language, it seems to me that I have to define these types and macros by myself in Julia, and make sure that they are exactly the same as in the fftw3.h file.

It is possible to do these for one or two times but can become very difficult when there are many types and macros and when they are very complicated types.

Since all the information has already been included in the .h file, is there a way to load the .h file, and automatically define all these different types and macros in Julia?

2 Likes