Python调用C/C++初步
来源:互联网 发布:新加坡 经济形势 数据 编辑:程序博客网 时间:2024/06/05 03:48
原文地址:http://daweibalong.iteye.com/blog/1660181
测试库要求做到全部自动化–动态添加新的计算图像指标可以直接不用重写底层java程序……这段时间在学Python,由于Python的ctypes可以试python轻松调用动态链接库,从而调用c/c++程序,于是想到可以在添加指标的时候有管理员再上传相关方法的dll或so文件,由Python进行调用新的指标计算方法进行重新计算。不知效果如何,先测试简单的调用:
1、编写test.c
#include <stdlib.h> int foo(int a, int b) { printf("Your input %i and %i\n", a, b); return a + b; }
2、 gcc编译:gcc -o test.so -shared -fPIC test.c
3、 编写test.py
import ctypes ll = ctypes.cdll.LoadLibrary lib = ll("./test.so") lib.foo(1, 3)
4、运行
python test.py
成功运行 Python调用C/C++初步
不过在调用c++文件的时候会发生错误,具体原因不详,但依然可以调用!!!:
1、编写c++文件test2.cpp
Cpp代码 收藏代码
#include<iostream> using namespace std; void foo2(int a,int b) { cout<<a<<" "<<b<<endl; }
//以下为必须
Cpp代码 收藏代码
extern "c" { void cfoo2(int a,int b) { foo2(a,b); } }
2、编译c++文件:
g++ -o test2.so -shared -fPIC test2.c
3、 编写test2.py
import ctypes ll = ctypes.cdll.LoadLibrary lib = ll("./test2.so") lib.cfoo2(1, 3) ``` 4、运行:python test2.py成功!问题补充:1、在windows下调用dll,如果过python是64位,那么在写dll时编译要用x64,要不然会出现错误的win32提示。2、在h文件中:Cpp代码 收藏代码
extern "C" int __declspec(dllexport)add(int x,int y);
cpp:Cpp代码 收藏代码
int __declspec(dllexport)add(int x,int y) { cout<<x<<" "<<y<<endl; return x+y; }
“`
0 0
- Python调用C/C++初步
- BCB(C++)调用Python
- C中调用PYTHON
- C调用python
- C调用python
- Python 调用 C函数
- python调用c
- Python 调用C
- c中调用python
- Python调用c/c++
- c 调用python
- python 调用 c 函数
- python调用C函数库
- Python调用C
- Python调用C/C++
- Python调用C程序
- python调用c/c++
- python调用c
- project euler 21
- Concurrent包
- 线程使用经验
- 性能-客户端测试(二)
- 用户行为分析业务系统日志处理方案
- Python调用C/C++初步
- 一起学libcef--源码文件介绍(VS2015运行DEMO)
- 数据结构与算法(线性表_顺序表存储结构)
- project euler 22
- JAVA 栈数据结构及其应用
- asio1
- js回到顶部,关闭一个模块的方法
- IOS OC 字典
- PAT甲级1005