[英]Embedded python in c++ on raspberry pi
Before I asked for how to call c++ in python, but that's quite difficult to me. 在我问如何在python中调用c ++之前,但这对我来说很难。 And later on I found that if calling python in c++ seems much more easily.
后来我发现,如果在c ++中调用python似乎要容易得多。 As I followed the tutorial on codeproject , I've got the following problem while I compile it.
在我遵循codeproject教程时 ,在编译它时遇到了以下问题。
"pi@raspberrypi ~/New $ g++ led.cpp"
led.cpp:3:20: fatal error: Python.h: No such file or directory
compilation terminated.
I searched solutions whole days. 我整天都在寻找解决方案。 I tried
sudo apt-get install python-dev
(even other versions, eg. 2.7, 3.2, etc..), but everything are already completely installed and still get the error. 我尝试了
sudo apt-get install python-dev
(甚至其他版本,例如2.7、3.2等。),但一切都已经完全安装,仍然出现错误。 I can found the location of Python.h by find function of my raspberry pi. 我可以通过树莓派的find函数找到Python.h的位置。
Any finally I found the following solution on some site: 最终我在某个站点上找到了以下解决方案:
pi@raspberrypi ~/New $ g++ $(python-config --includes) led.cpp
Python.h fatal error dismissed, and following error comes. 消除了Python.h致命错误,并出现以下错误。
/tmp/ccSIJpeH.o: In function `main':
led.cpp:(.text+0x30): undefined reference to `Py_Initialize'
led.cpp:(.text+0x44): undefined reference to `PyString_FromString'
led.cpp:(.text+0x54): undefined reference to `PyImport_Import'
led.cpp:(.text+0x64): undefined reference to `PyModule_GetDict'
led.cpp:(.text+0x84): undefined reference to `PyDict_GetItemString'
led.cpp:(.text+0x94): undefined reference to `PyCallable_Check'
led.cpp:(.text+0xbc): undefined reference to `PyObject_CallObject'
led.cpp:(.text+0xc4): undefined reference to `PyErr_Print'
led.cpp:(.text+0x158): undefined reference to `Py_Finalize'
collect2: ld returned 1 exit status
Please tell me how to solve this! 请告诉我如何解决这个问题!
Addendum : Thank you Nitori answer my question. 附录 :谢谢Nitori回答我的问题。 I can compile it now.
我现在可以编译它。 After compiled it, it created an "a.out" file, but when I runs it it done nothing...
编译后,它创建了一个“ a.out”文件,但是当我运行它时,它什么也没做...
This is the code of led.cpp 这是led.cpp的代码
// python functions from C code
//
#include <Python.h>
int main(int argc, char *argv[])
{
PyObject *pName, *pModule, *pDict, *pFunc, *pValue;
if (argc < 3)
{
printf("Usage: exe_name python_source function_name\n");
return 1;
}
// Initialize the Python Interpreter
Py_Initialize();
// Build the name object
pName = PyString_FromString(argv[1]);
// Load the module object
pModule = PyImport_Import(pName);
// pDict is a borrowed reference
pDict = PyModule_GetDict(pModule);
// pFunc is also a borrowed reference
pFunc = PyDict_GetItemString(pDict, argv[2]);
if (PyCallable_Check(pFunc))
{
PyObject_CallObject(pFunc, NULL);
} else
{
PyErr_Print();
}
// Clean up
Py_DECREF(pModule);
Py_DECREF(pName);
// Finish the Python Interpreter
Py_Finalize();
return 0;
}
Here is my python code to call. 这是我要调用的python代码。 led.py
led.py
import RPi.GPIO as GPIO
import time
def ledopen():
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(27, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.output(27, True)
GPIO.output(22, True)
GPIO.output(18, True)
GPIO.output(23, True)
GPIO.output(24, True)
GPIO.output(17, True)
time.sleep(5)
GPIO.output(27, False)
GPIO.output(22, False)
GPIO.output(18, False)
GPIO.output(23, False)
GPIO.output(24, False)
GPIO.output(17, False)
GPIO.cleanup()
return
ledopen()
and I called the program by entering "sudo ./a.out led led ledopen". 然后我输入“ sudo ./a.out led led ledopen”来调用该程序。 And it done nothing.
它什么也没做。
You also need to link against Python. 您还需要针对Python进行链接。 For the way you are compiling this should work:
g++ $(python-config --includes --libs) led.cpp
对于您的编译方式,它应该可以正常工作:
g++ $(python-config --includes --libs) led.cpp
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.