Hi I am trying to create a python binding for a little stack machine I wrote and chose to try swig for it.
The problem is that the generated code wont compile because some classes are unknown. The reasons for that is, that my classes are all inside a namespace.
The class I want to create a wrapper for looks like this:
#ifndef STACK_MACHINE_H
#define STACK_MACHINE_H
#include <stack.h>
#include <operand.h>
#include <instruction.h>
#include <program.h>
namespace stackmachine
{
class stack_machine
{
public:
stack_machine() = default;
void run();
void load (instruction* op);
vrml_variable* get_stack_top();
private:
stack m_stack;
program m_code;
};
}
#endif
The first problem is the load
method. The instruction
class is defined in one of the headers, and is also inside the stackmachine
namespace:
#ifndef INSTRUCTION_H
#define INSTRUCTION_H
#include <stack.h>
namespace stackmachine
{
class instruction
{
public:
virtual ~instruction() = 0;
virtual void execute(stack& current_stack) = 0;
};
inline instruction::~instruction()
{
}
}
#endif
Here is a snipped of generated code:
SWIGINTERN PyObject *_wrap_stack_machine_load(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
stackmachine::stack_machine *arg1 = (stackmachine::stack_machine *) 0 ; // namespace used => OK!
instruction *arg2 = (instruction *) 0 ; //namespace ignored => error
void *argp1 = 0 ;
int res1 = 0 ;
void *argp2 = 0 ;
Now the interface definition is as simple as possible and probably the place where I need to make changes, but so far I have not found a hint on what to do:
%module stackmachine
%{
#include <stack_machine.h>
%}
%include <stack_machine.h>
Adding the instruction.h to the includes does not change anything.
It'd be great if someone could point me in the right direction.
Thanks.
EDIT:
I did try using "includeall" and it changed the problem, now the instruction class is known but standard c++ headers like vector and string wont be found. And I don't really want to include all available classes in the wrapper.
Any class/function/global in the public interface may need a %include
to generated wrapper code. SWIG does not recursively include without -includeall
but that's not desirable as that would try to wrap system headers.
Order matters as well. SWIG needs to generate wrapper code for instruction
before stack_machine
since it is used by stack_machine
.
%module stackmachine
%{
#include <stack_machine.h>
%}
// as needed...
%include <stack.h>
%include <operand.h>
%include <instruction.h>
%include <program.h>
%include <stack_machine.h>