13

We have a vendor that has provided us a C++ library and headers, that I'm trying to wrap using SWIG. It appears that they are being too clever by a half with the preprocessor directives:

// top.h
#define DECLARE_WITH_COMMA(a) a,

and then

// foo.h
#include "top.h"

#define MY_TYPES(d) \
  d(One)   \
  d(Two)   \
  d(Three) \
  NumElems

enum MyTypes {
  MY_TYPES(DECLARE_WITH_COMMA)
};

Which is all a longwinded way of saying that when I try to run SWIG (version 2.0.4) on "foo.h", I get:

foo.h:12: Error: Syntax error in input(1).

So my question is what are my options here, given that I probably don't want to change the vendor-supplied headers?

laslowh
  • 8,482
  • 5
  • 34
  • 45

1 Answers1

12

SWIG doesn't recurse into nested headers by default, so your .i file should look something like:

%module mymod

%{
#include "foo.h"
%}

%include "top.h"
%include "foo.h"

There is also a SWIG switch:

-includeall     - Follow all #include statements

but if you have system headers that may do more than you intend.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • 1
    @MarkTolonen, what if `top.h` has an `#include` statement for another header file? I'm having this issue. I tried adding an `%include` statement in my `.i` file but I'm getting the same syntax error. – mark jay Dec 29 '16 at 21:00
  • @citizenSNIPS Keep adding `%include` for any header you need to process. SWIG doesn't recurse. – Mark Tolonen Dec 29 '16 at 21:49
  • 1
    I've found that adding `%include ` in the interface file can resolve this issue. see section 3.4 in the swig documentation [HERE](http://www.swig.org/Doc1.3/Windows.html) – mark jay Dec 31 '16 at 19:18