[問題] C/C++ 輸出動態連結給python呼叫

作者: hardman1110 (笨小孩)   2018-01-18 14:43:05
開發平台(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):
作者: cole945 (躂躂..)   2018-01-18 15:04:00
extern "C" void cfun (...) symbol mangling 問題
作者: hardman1110 (笨小孩)   2018-01-18 15:16:00
u save my day~用extern "C"{}把內容包起來就好了~~
作者: sunev (Veritas)   2018-01-18 16:06:00
順便問一下,除了extern C 沒有其它解法嗎?
作者: gozule (好冷啊~~)   2018-01-18 16:31:00
如果是要自已寫c的func處理numpy的array,建議使用cython如果是要call lib, cython也是很好的選擇cython的numpy範例 https://goo.gl/xfR2XA
作者: mikukonn (mikukon)   2018-01-18 17:23:00
可以用用看boost python
作者: lc85301 (pomelocandy)   2018-01-20 16:55:00
C的話也許能用 ctypes
作者: schizophrena (你很記者你很腦殘)   2018-01-20 23:12:00
boost python現在只有支援python2.6 有點麻煩QQ現在python大部份都用python3
作者: Sanvean   2018-01-22 08:36:00
boost python 應該支援 python3 喔https://github.com/TNG/boost-python-examples
作者: Ommm5566 (56天團)   2018-01-22 09:40:00
推樓上 boost1.56開始就支援py3 超爽der~~~

Links booklink

Contact Us: admin [ a t ] ucptt.com