相关疑难解决方法(0)

试图理解编写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("Error importing module.");
           exit(-1);
      }


      pFunc   = PyObject_GetAttrString(pModule, "Hello"); 
      PyEval_CallObject(pFunc, NULL); 
      Py_Finalize(); 
      return 0; 
 }
Run Code Online (Sandbox Code Playgroud)

文件"hello.py"只包含内容:

 def Hello():
     print "Hello world!"
Run Code Online (Sandbox Code Playgroud)

注意:我已经安装了python2.7-dev和python-dev以及libboost-python-dev.但是当我去编译代码时,我得到错误,我认为是由于错误地链接到Python库.

 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): …
Run Code Online (Sandbox Code Playgroud)

c++ python

7
推荐指数
1
解决办法
3933
查看次数

标签 统计

c++ ×1

python ×1