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

看板C_and_CPP作者 (笨小孩)時間6年前 (2018/01/18 14:43), 編輯推噓6(607)
留言13則, 9人參與, 6年前最新討論串1/1
開發平台(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

01/18 15:04, 6年前 , 1F
extern "C" void cfun (...) symbol mangling 問題
01/18 15:04, 1F

01/18 15:16, 6年前 , 2F
u save my day~用extern "C"{}把內容包起來就好了~~
01/18 15:16, 2F

01/18 16:06, 6年前 , 3F
順便問一下,除了extern C 沒有其它解法嗎?
01/18 16:06, 3F

01/18 16:31, 6年前 , 4F
如果是要自已寫c的func處理numpy的array,建議使用cython
01/18 16:31, 4F

01/18 16:32, 6年前 , 5F
如果是要call lib, cython也是很好的選擇
01/18 16:32, 5F

01/18 16:35, 6年前 , 6F
cython的numpy範例 https://goo.gl/xfR2XA
01/18 16:35, 6F

01/18 17:23, 6年前 , 7F
可以用用看boost python
01/18 17:23, 7F

01/20 16:55, 6年前 , 8F
C的話也許能用 ctypes
01/20 16:55, 8F

01/20 23:12, 6年前 , 9F
boost python現在只有支援python2.6 有點麻煩QQ
01/20 23:12, 9F

01/20 23:13, 6年前 , 10F
現在python大部份都用python3
01/20 23:13, 10F

01/22 08:36, 6年前 , 11F
boost python 應該支援 python3 喔
01/22 08:36, 11F

01/22 08:36, 6年前 , 12F

01/22 09:40, 6年前 , 13F
推樓上 boost1.56開始就支援py3 超爽der~~~
01/22 09:40, 13F
文章代碼(AID): #1QO47-F_ (C_and_CPP)