在C ++中嵌入python函数 [英] Embed python function in C++

查看:70
本文介绍了在C ++中嵌入python函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Cython从python生成c代码,但是名称修改似乎存在一些问题。我首先生成将代码从python转换为c代码,然后使用gcc将代码编译为.so。我想使用cython而不是C / python API的原因是因为稍后我将在更复杂的类上使用它,而我希望稍后再将其用作速度库(我很难找到去世的人从python到C ++,因为通常是相反的方式。以下是我必须尝试执行的所有代码(但失败了)。任何输入将不胜感激。

I am experimenting with Cython to generate c code from python but there seems to be some issues with name mangling. I first generate convert the code from python to c code and then I compile the code using gcc into a .so . The reason I want to use cython instead of C/python API is because I will be later using this on more complicated classes that I would like to be a library for speed etc later on (I am having a lot of trouble finding people who go from python to C++ since it is usually the other way around). Below is all the code that I have to try to execute the code (but fails). Any input will be appreciated. Thanks!

#hello.pyx
def say_hello():
    print "Hello World!"

#generate the c code
cython -a hello.pyx

#creates the shared library
gcc -shared -pthread -fPIC -fwrapv -O2 -Wall -fno-strict-aliasing -I/usr/include/python2.6 -o libhello.so hello.c

//temp.cpp
#include <iostream>
extern "C" {
void say_hello();
};

using namespace std;

int main(){
    say_hello();
    return 1;
};

#attempt to compile (this is where it fails)
g++ -I/usr/include/python2.6/ -lpython2.6 -L./ -lhello temp.cpp -o temp

以下是错误消息:

/tmp/ccpKHOMl.o: In function main: temp.cpp:(.text+0x5): undefined reference to say_hello' /tmp/ccpKHOMl.o: 
In function __static_initialization_and_destruction_0(int, int): 
  temp.cpp:(.text+0x33): undefined reference to std::ios_base::Init::Init()  
  temp.cpp:(.text+0x38): undefined reference to std::ios_base::Init::~Init() 
collect2: ld returned 1 exit status 


推荐答案

您将无法以这种方式获得所需的互操作性。如果打开并检查hello.c,则在任何地方都找不到 static int say_hello。 Cython的设计宗旨是让Python使用C库,而不是让C库使用python。

You're not going to be able to get the interoperation you want that way. If you open and inspect hello.c you won't find "static int say_hello" anywhere in there. Cython is designed for letting Python use C libraries, not letting C libraries use python.

您可以在文档中查看,但不幸的是,这种支持仍然是针对python解释器的就是负责,而您正在寻找的是相反的方法。

You can look here in the documentation, but unfortunately this support is still for a python interpreter that is "in charge" and what you're looking for is the other way around.

http://docs.python.org/release/2.5.4/ext/callingPython.html

还有在其他应用程序中嵌入Python的入门书

There's also the primer on "Embedding Python in Another Application"

http://docs.python.org/2/extending/embedding.html

I不知道您的要求是什么,但是在某些情况下,您可以成功地将数据写入文件,调用Python程序进行咀嚼,然后从另一个文件中解析结果。比在内存中保留内容要难一些,而且速度要慢一些,但是在许多情况下完全可以使用。

I don't know what your requirements are, but in some cases you can successfully write data to a file, call a Python program to chew on it, then parse the results from another file. It's a little ugly and slower than keeping things in memory but it's entirely workable in many situations.

这篇关于在C ++中嵌入python函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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