2023-04-21

swig python derived and base in different modules

I am trying to reproduce a python example from the swig 4.0 documentation Modules Basics. I got properly generated _base_module.so and _derived_module.so . However when I try to use derived module from my test program

#!/usr/bin/env python3

import sys
import derived_module
...

I got an import error:ImportError: /homes/../_derived_module.so: undefined symbol: _ZTI4Base

The reason for the error is quite understandable but it is not clear how to avoid it. Below are my files.

base.h:

#ifndef _BASE_H_INCLUDED_
#define _BASE_H_INCLUDED_

class base {
public:
    virtual int foo();
};

#endif // _BASE_H_INCLUDED_

base.cpp:

#include "base.h"

int base::foo() { return 10; }

base_module.i:

%module base_module

%{
#include "base.h"
%}
%include base.h

derived_module.i:

%module derived_module

%{
#include "base.h"
%}

%import(module="base_module") "base.h"

%inline %{
class derived : public base {
public:
    int foo() override { return 20; }
};
%}

test.py:

#!/usr/bin/env python3

import sys
import derived_module

if __name__ == "__main__":
    derived = derived_module.derived();
    res = derived.foo();
    print("res={}".format(res));
    sys.exit()

Makefile:

SWIG = swig4
CCX = g++

LD = g++

all: derived_module.py

derived_module.py: base.h base.cpp base_module.i derived_module.i
        ${SWIG} -python -py3 -c++ -cppext cpp base_module.i
        ${SWIG} -python -py3 -c++ -cppext cpp derived_module.i
        ${CCX} -O2 -fPIC -c base.cpp
        ${CCX} -O2 -fPIC -c base_module_wrap.cpp -I/usr/include/python3.8
        ${CCX} -O2 -fPIC -c derived_module_wrap.cpp -I/usr/include/python3.8
        ${LD} -shared base.o base_module_wrap.o -o _base_module.so
        ${LD} -shared derived_module_wrap.o -o _derived_module.so

run: derived_module.py
        python3 ./test.py

clean:
        rm -rf *~ derived_module.py base_module.py *_wrap.* *.o *.so __pycache__


No comments:

Post a Comment