Reputation: 379
I'm a noob who is new on Python . I'm working on a project which uses Python to read sensor. I want to analyze the results using c++ code. Is there a way to sends the results of python sensor reading so that c++ code can read it? thanks in advance.
Upvotes: 1
Views: 882
Reputation: 4031
You have many solution to do so:
> #include<stdio.h> > #include<stdlib.h> system("my_bash_script.sh");
if you want to work in the other direction you can embed some C++ code in your Python. In the past I did this by using Swig: http://www.swig.org/tutorial.html
Upvotes: 1
Reputation: 94339
There are a couple of options for combining Python and C++:
You could implement the C++ logic in a separate process and have your Python code invoke that program. This is very easy to implement, but in case the data exchange between the two parts needs to be highly efficient and/or if there is complex data is passed, you will have to think about how to serialize data back and forth correctly and efficiently.
You could implement a Python module in C++ and then load that into the Python interpreter (i.e. the Python interpreter hosts the C++ code). This is not entirely trivial (see Extending Python with C or C++) but avoids all the problems associated with process control which the previous idea implies.
You could write a C++ program which embeds the Python interpreter and takes care of evaluatin (i.e. the C++ code hosts the Python interpreter). This is not very difficult (see Embedding Python in Another Application) and mostly makes sense if the majority of your code in C++ and it's just a few smaller parts for which you want to use Python.
Upvotes: 2