python调用c++接口:Python调用C++接口实现功能的强大性

示例示例Python调用C++接口的方法有很多,下面介绍一种使用Cython的方法:安装Cython:

示例示例Python调用C++接口的方法有很多,介绍一种使用Cython的方法:安装Cython:

Python调用C++接口的方法有很多,介绍一种使用Cython的方法:

1、安装Cython:

pip install cython

2、创建C++源文件,如test.cpp:

#include

using namespace std;

extern "C" int add(int a, int b) {

return a + b;

}

3、创建Cython文件,如test.pyx:

cdef extern from "test.h":

int add(int a, int b)

def py_add(int a, int b):

return add(a, b)

4、编译Cython文件:

cython test.pyx

5、编译C++源文件:

g++ -c -fPIC test.cpp -o test.o

6、生成动态链接库:

g++ -shared -Wl,-soname,libtest.so -o libtest.so test.o

7、在python中调用:

import test

print test.py_add(2, 3)

上述步骤执行完成后,就可以在python中调用C++接口了。

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(45)
java语言历史从1995年到今日的发展历程
上一篇
gcc 编译选项:使用GCC的-O2优化编译技术
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(32条)