作者hardman1110 (笨小孩)
看板C_and_CPP
标题[问题] C/C++ 输出动态连结给python呼叫
时间Thu Jan 18 14:43:05 2018
开发平台(Platform): (Ex: Win10, Linux, ...)
Win10 and Ubuntu 16
编译器(Ex: GCC, clang, VC++...)+目标环境(跟开发平台不同的话需列出)
GCC, VC++ and g++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
None
问题(Question):
目的:
想在python中呼叫C/C++C函式对Numpy array 做运算
现况:
目前参考下面连结实作也成功了
https://goo.gl/EcUqc7
程式放在:
https://github.com/ChiFang/question/tree/master/python_call_c
p.s 为了在windows也能跑我有加了 DLL_EXPORT
问题:
无论在linux还是windows上 只要把.c改成.cpp或使用g++编译
python 呼叫产生的动态连结都会出现
AttributeError: ./ctest.so: undefined symbol: cfun >> on linux
AttributeError: function 'cfun' not found >> on windows
请问各位大大 我是否还少做了甚麽设定? 恳请指点
喂入的资料(Input):
从python 传入numpy阵列给C/C++函式做处理
预期的正确结果(Expected Output):
所有阵列数值变2倍
错误结果(Wrong Output):
使用动态连结时找不到函式
程式码(Code):(请善用置底文网页, 记得排版)
ctest.c:
#include <stdio.h>
#ifdef _MSC_VER
#define DLL_EXPORT __declspec( dllexport )
#else
#define DLL_EXPORT
#endif
DLL_EXPORT void cfun(const void * indatav, int rowcount, int colcount, void *
outdatav)
{
//void cfun(const double * indata, int rowcount, int colcount, double *
outdata) {
const double * indata = (double *)indatav;
double * outdata = (double *)outdatav;
int i;
puts("Here we go!");
for (i = 0; i < rowcount * colcount; ++i) {
outdata[i] = indata[i] * 2;
}
puts("Done!");
}
补充说明(Supplement):
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.34.230.27
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1516257790.A.3FF.html
1F:推 cole945: extern "C" void cfun (...) symbol mangling 问题 01/18 15:04
2F:→ hardman1110: u save my day~用extern "C"{}把内容包起来就好了~~ 01/18 15:16
3F:推 sunev: 顺便问一下,除了extern C 没有其它解法吗? 01/18 16:06
4F:→ gozule: 如果是要自已写c的func处理numpy的array,建议使用cython 01/18 16:31
5F:→ gozule: 如果是要call lib, cython也是很好的选择 01/18 16:32
7F:推 mikukonn: 可以用用看boost python 01/18 17:23
8F:推 lc85301: C的话也许能用 ctypes 01/20 16:55
9F:推 schizophrena: boost python现在只有支援python2.6 有点麻烦QQ 01/20 23:12
10F:→ schizophrena: 现在python大部份都用python3 01/20 23:13
11F:→ Sanvean: boost python 应该支援 python3 喔 01/22 08:36
13F:推 Ommm5566: 推楼上 boost1.56开始就支援py3 超爽der~~~ 01/22 09:40