Summary:
I have a server using BottlePy that hosts a web-interface for the app I'm working on. The back-end was written in C++ because the original front-end was written using GTK+3. Since it was written using GTK+3, the front-end has a lot of signals to send to the back-end to trigger back-end functionality. I want to allow my Python server to send these same signals to the C++ back-end.
Approach:
I wasn't sure how to get started connecting a scripted language to C++, especially for dynamic requests and signalling, so I did some poking around and I learned about SWIG. It's my understanding that SWIG creates a wrapper/interface as the "bridge" between Python and another language (C++ in my case). I followed the tutorial here for connecting a Python module, and then I tried to run my server, but it didn't work.
Steps Taken:
Changed compile line from:
g++ -c $(CPPFLAGS) $^ -o $@ -rdynamic
To:
g++ -fPIC -c $^ -o $@ -rdynamic $(CPPFLAGS)
CPPFLAGS
uses pkg-config
to generate the dependencies for the files being compiled. I can give more detail here if needed. I followed this post which led me to move CPPFLAGS
to the end of the line, since I was getting this error:
ImportError: /path/to/file.so: undefined symbol: _ZNK4Glib5Error4whatEv
I also added -fPIC
which I believe I added after getting a shared library error that suggested it.
To set up the shared libraries, I created a .i
file, as suggested in the tutorial linked above. Then I ran swig -python file.i
, followed by gcc -shared *object files* -o outfile.so
. Finally, I added an import statement to my server file and added a call to the appropriate function from the imported module. When I tried to run the server, I got the error mentioned above.
Questions:
- Is there another way to link my Python server to my C++ backend?
- Did I miss a step in my procedure?
- Why would I be getting a
reference not defined
error using this method when my program compiles and runs successfully using the original front-end?
Additional Resources:
I made a similar post here.
Post about the linker.
Thanks in advance!