试图理解用于编写Python / C ++混合的链接过程 [英] Trying to understand linking procedure for writing Python/C++ hybrid

查看:128
本文介绍了试图理解用于编写Python / C ++混合的链接过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开始学习更多关于使用SWIG和其他方法来连接Python和C ++。为了开始,我想编译这个简单的程序,在另​​一篇帖子

  #include< Python.h> 

int main()
{
Py_Initialize();
PyRun_SimpleString(import sys; sys.path.insert(0,'/ home / ely / Desktop / Python / C-Python /')

PyObject * pModule = NULL;
PyObject * pFunc = NULL;

pModule = PyImport_ImportModule(hello);
if(pModule == NULL){
printf(导入模块错误);
exit(-1);
}


pFunc = PyObject_GetAttrString(pModule,Hello);
PyEval_CallObject(pFunc,NULL);
Py_Finalize();
return 0;
}

其中文件hello.py只有内容:

  def Hello():
printHello world!

注意:我已经有python2.7-dev和python-和libboost-python-dev安装。但是当我去编译代码,我得到的错误,我相信是由于不正确的链接到Python库。

  ely @AMDESK:〜/ Desktop / Python / C-Python $ gcc -I / usr / include / python2.7 test.cpp /tmp/ccVnzwDp.o:在main函数中:
test.cpp :(。文本+ 0x9):未定义引用`Py_Initialize'
test.cpp :( .text + 0x23):未定义引用`PyImport_ImportModule'
test.cpp :(。 ```PyObject_GetAttrString'
test.cpp :(。text + 0x72):未定义的引用`PyEval_CallObjectWithKeywords'
test.cpp :(。text + 0x77):未定义引用'Py_Finalize'
/ tmp / ccVnzwDp.o :(。eh_frame + 0x12):未定义的引用`__gxx_personality_v0'
collect2:ld返回1退出状态

我在这里在线钓鱼的例子,我发现了以下语法,这导致代码编译成一个对象文件,但是我无法实际执行该文件。 p>

  ely @ AMDESK:〜/ Desktop / Python / C-Python $ gcc -c -g -I / usr / include / python2。 7 test.cpp 
ely @ AMDESK:〜/ Desktop / Python / C-Python $ ./test.o
bash:./test.o:Permission denied
ely @ AMDESK:〜 / Desktop / Python / C-Python $ chmod ug = rx ./test.o
ely @ AMDESK:〜/ Desktop / Python / C-Python $ ./test.o
bash:./test .o:不能执行二进制文件
ely @ AMDESK:〜/ Desktop / Python / C-Python $ sudo chmod ug = rx ./test.o
ely @ AMDESK:〜/ Desktop / Python / C -Python $ ./test.o
bash:./test.o:无法执行二进制文件

如果使用 g ++ 而不是 gcc ,仍会看到与上述相同的行为。



帮助我理解我在链接中的错误将是伟大的,甚至更好的任何解释,帮助我理解在我需要做的链接类型的逻辑,所以我我会记住更好的什么可能的事情,我忘记了下一次。谢谢!

解决方案

您看到的是链接器错误。要修复这些问题,您需要链接 python2.7 库。



尝试下一行:

  gcc -I / usr / include / python2.7 test.c -lpython2.7 
pre>

它应该可以工作。


I want to start learning more about using SWIG and other methods to interface Python and C++. To get started, I wanted to compile this simple program mentioned in another post:

#include <Python.h> 

 int main() 
 { 
      Py_Initialize(); 
      PyRun_SimpleString ("import sys; sys.path.insert(0, '/home/ely/Desktop/Python/C-Python/')");

      PyObject* pModule = NULL; 
      PyObject* pFunc   = NULL; 

      pModule = PyImport_ImportModule("hello");
      if(pModule == NULL){
           printf("Error importing module.");
           exit(-1);
      }


      pFunc   = PyObject_GetAttrString(pModule, "Hello"); 
      PyEval_CallObject(pFunc, NULL); 
      Py_Finalize(); 
      return 0; 
 }

where the file "hello.py" just has the contents:

 def Hello():
     print "Hello world!"

Note: I already have python2.7-dev and python-dev and libboost-python-dev installed. But when I go to compile the code, I get errors that I believe are due to incorrectly linking to the Python libraries.

 ely@AMDESK:~/Desktop/Python/C-Python$ gcc -I/usr/include/python2.7 test.cpp    /tmp/ccVnzwDp.o: In function `main':
 test.cpp:(.text+0x9): undefined reference to `Py_Initialize'
 test.cpp:(.text+0x23): undefined reference to `PyImport_ImportModule'
 test.cpp:(.text+0x58): undefined reference to `PyObject_GetAttrString'
 test.cpp:(.text+0x72): undefined reference to `PyEval_CallObjectWithKeywords'
 test.cpp:(.text+0x77): undefined reference to `Py_Finalize'
 /tmp/ccVnzwDp.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
 collect2: ld returned 1 exit status

I was fishing around for examples of this online, and I found the following syntax, which causes the code to compile into an object file, but then I am unable to actually execute the file.

 ely@AMDESK:~/Desktop/Python/C-Python$ gcc -c -g -I/usr/include/python2.7 test.cpp 
 ely@AMDESK:~/Desktop/Python/C-Python$ ./test.o
 bash: ./test.o: Permission denied
 ely@AMDESK:~/Desktop/Python/C-Python$ chmod ug=rx ./test.o
 ely@AMDESK:~/Desktop/Python/C-Python$ ./test.o
 bash: ./test.o: cannot execute binary file
 ely@AMDESK:~/Desktop/Python/C-Python$ sudo chmod ug=rx ./test.o
 ely@AMDESK:~/Desktop/Python/C-Python$ ./test.o
 bash: ./test.o: cannot execute binary file

The same behavior as above is still seen if I use g++ instead of gcc.

Help in understanding my error in linking would be great, and even better for any sort of explanation that helps me understand the "logic" behind the kind of linking I need to do, so that I'll remember better what possible things I am forgetting the next time. Thanks!

解决方案

What you are seeing are linker errors. To fix those, you need to link python2.7 library.

Try next line :

gcc -I/usr/include/python2.7 test.c -lpython2.7

it should work.

这篇关于试图理解用于编写Python / C ++混合的链接过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆