Please check out Cython. It makes it very easy to wrap C code.
This is adapted from the documentation on calling external C functions:
cdef extern from "math.h":
double sin(double)
def pysin(x):
return sin(x)
You could then call pysin from the compiled module like a normal Python module.
Please check out Cython. It makes it very easy to wrap C code.
This is adapted from the documentation on calling external C functions:
cdef extern from "math.h":
double sin(double)
def pysin(x):
return sin(x)
You could then call pysin from the compiled module like a normal Python module.
I would definitely go with boost.python, which provides even cleaner wrappers. If you dislike the idea of using C++, then Cython is a good alternative.
How do you call Python code from C code? - Stack Overflow
How to Embed Python in C++?
how to compile and run C code with embedded python script? - Stack Overflow
Using embedded C library in Python emulation - Stack Overflow
I recommend the approaches detailed here. It starts by explaining how to execute strings of Python code, then from there details how to set up a Python environment to interact with your C program, call Python functions from your C code, manipulate Python objects from your C code, etc.
EDIT: If you really want to go the route of IPC, then you'll want to use the struct module or better yet, protlib. Most communication between a Python and C process revolves around passing structs back and forth, either over a socket or through shared memory.
I recommend creating a Command struct with fields and codes to represent commands and their arguments. I can't give much more specific advice without knowing more about what you want to accomplish, but in general I recommend the protlib library, since it's what I use to communicate between C and Python programs (disclaimer: I am the author of protlib).
Have you considered just wrapping your python application in a shell script and invoking it from within your C application?
Not the most elegant solution, but it is very simple.
Background: Hello! I am working on a project that will compute physics, graphics, and AI/ML tasks. I want to handle the physics and graphics calculations with C++ and the AI/ML calculations with Python. I have learned that you can embed Python in C++ recently but seem to be having trouble getting the embedding to work. I spent sometime away from computer science - about a year - and decided to comeback recently. I made the discovery that I like the theoretical side of computer science and want to conduct research. I am actually going to graduate school in computer science soon and I am very excited. I want to use this project to jump start my Ph.D.
Setup: Visual Studio Code on Windows 11
Questions: What is the best way to run and access Python scripts from C++ and how exactly can this be accomplished? I've tried inserting pybind11 as a submodule to my project git repository (I used this resource), but it has trouble finding certain header files, like Python.h - is there another solution that doesn't need pybind11? Is embedding Python in C++ even worth it? Should I just stick to using just one language or the other?
Thank you!
My advice would be to wrap it.
Reasons for that:
- if you convert function by function, you'll introduce new bugs (we're just human) and this kind of stuff is pretty hard to test
- wrapping for python is done easily, using swig or even ctypes to load a dll on the fly, you'll find tons of tutorial
- if your lib gets updated, you have less impact in the long term.
However, you need to
- check that the license you purchase allows you to do that
- know that having same implementation on embedded and PC side, it won't help tracking bugs
- you might have a bit less portability than a full python implementation (anyway, not much of a point for you as your low layer needs to be rewritten per target)
Definitely wrap it. It might be as easy are running ctypesgen.py and then using it. Check this blog article about using ctypesgen to create a wrapper for libreadline http://wavetossed.blogspot.com/2011/07/asynchronous-gnu-readline.html in order to get access to the full API.