1

I would like to use Swig to generate C# wrappers for my c++ classes. I am facing the following problem:

I have defined an enum that uses values from a third party (closed source) library. The values are declared inside HEADER_FROM_3RD_PARTY_LIB.h as

#define 3RD_PARTY_LIB_CONST_VALUE_1 -4
#define 3RD_PARTY_LIB_CONST_VALUE_1 -10

My header file looks like:

#include "HEADER_FROM_3RD_PARTY_LIB.h"

namespace Foo
{
    namespace Bar
   {    
    class MyClass
    {       
     public:    
            enum MyEnum
        {           
        Enum1 = 3RD_PARTY_LIB_CONST_VALUE_1,            
        Enum2 = 3RD_PARTY_LIB_CONST_VALUE_2     
        };
        }
    }
}

I am using the following swig code:

%module cpp  

%{  
    #include "MyClass.h"  
%}  

%include <windows.i>  
%include "MyClass.h"  

The wrapper is successfully generated but the generated csharp file has the following enum generated:

public enum MyEnum
{           
    Enum1 = 3RD_PARTY_LIB_CONST_VALUE_1,            
    Enum2 = 3RD_PARTY_LIB_CONST_VALUE_2     
}

Obviously this generates an error since C# cannot find the 3RD_PARTY_LIB_CONST_VALUE_1 and 3RD_PARTY_LIB_CONST_VALUE_2 values.

Any ideas who could this be resolved? I had a look at Swig examples but could not find something similar.

2
  • That's rather odd. Have you tried building MyClass.cpp and it builds fine? The 3RD_PARTY_LIB_CONSTs are they anoter enum, or a #define, or static const or what? Please update your question not in comment.
    – Oliver
    Feb 1, 2014 at 12:57
  • Thanks for looking at this. I have updated my question and provided a temporary solution, but I am not happy with the result. Feb 1, 2014 at 15:28

1 Answer 1

1

The only solution I found so far was either to include

#include "HEADER_FROM_3RD_PARTY_LIB.h"

in the swig file or redefine the values again in the swig file

#define 3RD_PARTY_LIB_CONST_VALUE_1 -4
#define 3RD_PARTY_LIB_CONST_VALUE_1 -10

Any other suggestions how this could be improved? The problem with the first approach is that by including the entire header file I get lots of auto generated files that I do not need.

1
  • I agree, but +1 for finding a solution and posting it.
    – Oliver
    Feb 2, 2014 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.