我正在构建一个R扩展,其中嵌入了python。
现在一切都很顺利,除了python找不到我需要的编码。当我做一些涉及“Big5”的事情时,它一直抛出LookupError。但是,如果我构建一个独立的c++应用程序,python解释器会找到编码并停止抛出错误。
C中的普通独立示例的test.cpp:
#include <Python.h>
int main(int argc, char* argv[]) {
Py_SetProgramName("test"); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString(
"import codecs\n"
"f = codecs.open('big5_encoded_file', encoding='big5', mode='r')"
);
Py_Finalize();
return 0;
}R扩展的testr.cpp:
#include <R.h>
#include <Rdefines.h>
#include <Python.h>
extern "C" SEXP testpy();
SEXP testpy() {
Py_SetProgramName("test"); /* optional but recommended */
Py_Initialize();
PyRun_SimpleString(
"import codecs\n"
"f = codecs.open('big5_encoded_file', encoding='big5', mode='r')"
);
Py_Finalize();
return R_NilValue;
}ubuntu 12.10上的一个Makefile:
all: test testr.so
test: test.cpp
g++ test.cpp -o test -I/usr/include/python2.7 -lpython2.7
testr.so: testr.cpp
R CMD SHLIB testr.cpp编码运行正常,但Rscript -e "dyn.load('testr.so');.Call('testpy')"生成"LookupError: unknown ./test:big5“
谢谢
-编辑--
要构建testr.so,请设置:
export PKG_CXXFLAGS=-I/usr/include/python2.7
export PKG_LIBS=-lpython2.7发布于 2013-03-03 00:33:10
我注意到这是一个链接问题。
我尝试在嵌入的python中使用import encodings.big5,但是出现了undefined reference错误。http://bugs.python.org/issue4434中的解决方案适用于我:
在PyInitialize()之前
我可以调用
dlopen("libpython2.7.so", RTLD_LAZY | RTLD_GLOBAL);
https://stackoverflow.com/questions/15162685
复制相似问题