0

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.sois 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.

1 Answers1

0

In Python it changes because it changes in C++. But it changes the C++ value inside the Python process. Each process run in a completely separated context. So when you launch the C++ process separately it runs in completely other context, having its own set of variables, memory, registries. No matter what you do in a process will not affect the other process. No matter how many processes you launch, none will affect the content of the other. In order to send data from one process to another you need a way to communicate via Inter Process Communication (IPC). You send data into via files, sockets, pipes, RPC, messages.

armagedescu
  • 1,758
  • 2
  • 20
  • 31
  • 1
    This makes sense. I was hoping with SWIG I can connect those two applications and not just wrap C++ code to use it in other languages. – ColrblindMartian Jun 24 '20 at 15:39
  • A possibility would be embedding python into the c++ application and importing the python module from there. https://stackoverflow.com/questions/62919022/what-is-the-correct-way-to-import-a-python-module-wrapped-with-swig-in-c – ColrblindMartian Jul 20 '20 at 09:48
  • @marsmann007 You've done a good work. But it is not the answer. Try to launch Python, modify the value in C++ and check if you see changes in Python. You have to understand correctly that one program does not change the other in any mean. Loading C++ in Python is making a single program which is half Python and half C++. The same thing is when loading Python in C++. It is one single program, half Python and half C++. The only thing that changes there is what part is starting the program, the C++ or Python part. Everything else remains identically the same. – armagedescu Jul 20 '20 at 10:38