我想複製Flavian Coelho,linked here的工作。他使用Cython和Gnu科學圖書館(GSL)在Python中產生隨機數,從而大大加快了速度。當我輸入在Python我編譯用Cython代碼(用命令import cgibbs
),我收到以下錯誤:連接外部C庫時從Cython導入錯誤
ImportError: dlopen(./cgibbs.so, 2): Symbol not found: _gsl_rng_mt19937
Referenced from: /Users/wesley/scratch/cython/cgibbs.so
Expected in: dynamic lookup
你會發現,抱怨是符號_gsl_rng_mt19937
無法找到。我試圖鏈接到的函數被稱爲gsl_rng_mt19937
(無前導下劃線),這就是它在我的.pyx
文件中的顯示方式。我認爲Cython通過添加領先的下劃線以某種方式導致問題。
爲了使故障排除更容易,我已將代碼剝離下來併發布到下面。我的系統是:運行Python 2.7.2(32位)的Mac OSX 10.7(Lion),gcc-4.0(我用它編譯32位格式的GSL庫),GSL 1.15和Cython v0.15.1。
這裏是cgibbs.pyx的內容:
#declaring external GSL functions to be used
cdef extern from "math.h":
double sqrt(double)
cdef double Sqrt(double n):
return sqrt(n)
cdef extern from "gsl/gsl_rng.h":
ctypedef struct gsl_rng_type:
pass
ctypedef struct gsl_rng:
pass
gsl_rng_type *gsl_rng_mt19937
gsl_rng *gsl_rng_alloc(gsl_rng_type * T)
cdef extern from "gsl/gsl_randist.h":
double gamma "gsl_ran_gamma"(gsl_rng * r,double,double)
double gaussian "gsl_ran_gaussian"(gsl_rng * r,double)
cdef gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937)
錯誤消失,如果我註釋掉我cgibbs.pyx
的最後一行,但我不能實際使用的外部庫...任何您可以提供的洞察力表示讚賞。謝謝!
我不知道從SWIG--也許我應該這樣做。 – Wesley 2012-02-16 02:44:48