I am trying to build a Python wrapper around a C++ code using SWIG on Linux and I am not sure if the wrapper is being created correctly. As an example, here is a mini-problem (within my larger project).
Suppose I have a file called message.cpp
message.cpp
#include <iostream>
#include "message.hpp"
void warning (std::string message) {
using std::cout;
cout << "\n!! WARNING! " << message << " !!\n";
}
The corresponding header file is:
message.hpp
#include <string>
#include <fstream> // file I/O
#include <sstream>
#include <iostream>
void warning (const std::string message);
To pass this through SWIG, I have the following SWIG input file:
message.i
%module message
%{
#include "message.hpp"
%}
%include "message.hpp"
From Linux terminal, I create the wrapper using the following commands:
$ swig -c++ -python message.i
$ g++ -fpic -c message.hpp message_wrap.cxx message.cpp -I/usr/include/python2.7/ -I/usr/include
$ gcc -shared message_wrap.o message.o -o _message.so -lstdc++
All the 3 steps at the command prompt pass without any errors on the terminal and the following files are produced without any complaints:
message_wrap.cxx
message_wrap.o
message.o
message.py
message.hpp.gch
_message.so
I then try to test out the Python wrapper by entering the following commands on Linux terminal (staying in the same directory):
$ python
Python 2.7.17
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import _message
>>> _message.warning("SampleMessage")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: in method 'warning', argument 1 of type 'std::string const'
I was expecting it to simply display the line WARNING! SampleMessage
on the terminal, but instead it crashes. So it appears something has gone wrong. Can someone guide me where the problem lies please?