Search code examples
c++warningsswigsuppress-warnings

swig ignoring unknown base class


I am developing a c++ -> python wrapper using swig. I am trying to suppress Warning 401: Nothing known about base class 'xxx'. Ignored.

base.h:

#ifndef _BASE_H_INCLUDED_
#define _BASE_H_INCLUDED_

class Base {
public:
    Base() : result_(0) {}
    virtual ~Base() {}

    virtual void do_it(int n) {
        result_ = n + 2;
    }

protected:
    int result_;
};


#endif // _BASE_H_INCLUDED_

example.h:

#ifndef _EXAMPLE_H_INCLUDED_
#define _EXAMPLE_H_INCLUDED_

#include "base.h"

class Derived : public Base {
public:
    Derived() {}
    virtual ~Derived() {}

    void do_it(int n) override;
    int get_result() const;
};

#endif // _EXAMPLE_H_INCLUDED

example.cpp:

#include "example.h"

void Derived::do_it(int n) {
    result_ = n + 2;
}

int Derived::get_result() const {
    return result_;
}

example.i:

%module example

%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}

// %include base.h
%include example.h

If example.i has both base.h and example.h included it compiles and runs without problems.

If I comment out '%include base.h' (as shown) I have a warning that I could not suppress

example.h:6: Warning 401: Nothing known about base class 'Base'. Ignored.

I tried adding '%ignore Base;' in various places in the code and the warning was not suppressed.

Update: we do not want to suppress all 401 warnings from the command line.


Solution

  • You can define a dummy base class for SWIG to silence the warning for that particular class, and also ignore it so it isn't exposed to Python:

    %module example
    
    %{
    #define SWIG_FILE_WITH_INIT
    #include "example.h"
    %}
    
    %ignore Base;
    class Base {};
    %include example.h