看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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://www.ptt.cc/bbs/C_and_CPP/M.1516257790.A.3FF.html
cole945: extern "C" void cfun (...) symbol mangling 問題 01/18 15:04
hardman1110: u save my day~用extern "C"{}把內容包起來就好了~~ 01/18 15:16
sunev: 順便問一下,除了extern C 沒有其它解法嗎? 01/18 16:06
gozule: 如果是要自已寫c的func處理numpy的array,建議使用cython 01/18 16:31
gozule: 如果是要call lib, cython也是很好的選擇 01/18 16:32
gozule: cython的numpy範例 https://goo.gl/xfR2XA 01/18 16:35
mikukonn: 可以用用看boost python 01/18 17:23
lc85301: C的話也許能用 ctypes 01/20 16:55
schizophrena: boost python現在只有支援python2.6 有點麻煩QQ 01/20 23:12
schizophrena: 現在python大部份都用python3 01/20 23:13
Sanvean: boost python 應該支援 python3 喔 01/22 08:36
Ommm5566: 推樓上 boost1.56開始就支援py3 超爽der~~~ 01/22 09:40