I am not really sure if I'm taking the right path here.
(See Edit below)
I have a C++ library called myapp
that compiles into libmyapp.so
. This library contains a file called swigtest.h
.
The thing I want to do, is wrapping this file with SWIG to create an interface for python and using this interface to change or read specific values from myapp
.
Let's say myapp
is a console application that has some global variables like static int myGlobalVar = 5
.
In myapp
I have a function to read and to write the global variable:
int ReadGlobalVar() { return myGlobalVar; }
void SetGlobalVar(int value) { myGlobalVar = value; }
Here are my steps for compiling the module:
swig -c++ -python swigtest.i
g++ -fpic -c swigtest.cpp swigtest_wrap.cxx -L/pathToMyApp -lmyapp -I/usr/include/python3.6
g++ -Wall -Wextra -shared swigtest.o swigtest_wrap.o -o _swigtest.so -L/pathToMyApp -lmyapp
I open and load the python module as following:
LD_LIBRARY_PATH=/pathToMyApp python3
import swigtest
...
The problem I face know is, that python and my application are still separated from one another. If i modify the variable using the python module, it does not affect the c++ application and vice versa.
Is it even possible to link those two together or is SWIG only used to reuse C++ code in python?
I think the big problem here is that I have only a small understanding of how libraries are linked and created under C++.
I found a few tutorials on using SWIG with Python for example:
http://books.gigatux.nl/mirror/pythonprogramming/0596000855_python2-CHP-19-SECT-8.html
and https://www.geeksforgeeks.org/wrapping-cc-python-using-swig-set-1/
===================
Edit:
I created another example application for this scenario:
1) files:
main.cpp
#include <stdio.h> #include <unistd.h> #include <iostream> #include "Swigtest.h" using namespace std; int main() { Swigtest swigtest; for(;;) { sleep(1); cout << "Global Variable value:" << swigtest.ReadGlobalVar() << endl; } }
Swigtest.h
#ifndef SWIGTEST_H #define SWIGTEST_H static int myGlobalVar = 5; class Swigtest { public: Swigtest(); void SetGlobalVar(int var){ myGlobalVar = var; } int ReadGlobalVar() { return myGlobalVar; } }; #endif // SWIGTEST_H
Swigtest.cpp
#include "Swigtest.h" Swigtest::Swigtest() { }
Swigtest.i
%module swigtest
%{
#include "Swigtest.h"
%}
%include "Swigtest.h"
2) compile application
g++ -fpic -c Swigtest.cpp
g++ -fpic -c main.cpp
g++ Swigtest.o main.o -o libmyapp.so
3) compile python module
swig -c++ -python Swigtest.i
g++ -fpic -c Swigtest.cpp Swigtest_wrap.cxx -L. -lmyapp -I/usr/include/python3.6
g++ -Wall -Wextra -shared Swigtest.o Swigtest_wrap.o -o _swigtest.so -L. -lmyapp
now I have my application in myapp.so
and the python extension in _swigtest.so
4) executing test
executing application
I launch the application by executing it from the shell:
./libmyapp.so
Output:
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
Global Variable value:5
testing python module
in another terminal i open python (at the same path where libmyapp.so
is placed) and import the module
LD_LIBRARY_PATH=. python3
output:
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import swigtest
>>> a=swigtest.Swigtest()
>>> a.ReadGlobalVar()
5
>>> a.SetGlobalVar(3)
>>> a.ReadGlobalVar()
3
>>> a.ReadGlobalVar()
3
>>>
in the meantime the c++ application I launched from the terminal is happily outputting its value of 5.
What do I want?
I want that the change in the value also affects the c++ application. Above I changed the value from 5 to 3 in the python module, but the c++ application was unaffected from it.
Is this possible with SWIG? or am I doing something wrong here.