我在看here以查看如何向Python公開C++。我已經構建了Python深度學習代碼,它使用boost-python連接C++和python,並且它運行正常,所以我的系統有一些boost-python alread setup。 這裏是我的hello.cpp代碼(我使用WorldC和WorldP來清楚地顯示聲明中的C++和Python類名稱用法,我不知道爲什麼原始網頁使用相同的類名稱World
導致混淆初學者。)在Python中暴露C++類(只能加載ET_DYN和ET_EXEC)
#include <boost/python.hpp>
using namespace boost::python;
struct WorldC
{
void set(std::string msg) { this->msg = msg; }
std::string greet() { return msg; }
std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
class_<WorldC>("WorldP")
.def("greet", &WorldC::greet)
.def("set", &WorldC::set)
;
}
,這是我如何使hello.so
g++ -shared -c -o hello.so -fPIC hello.cpp -lboostpython -lpython2.7 -I/usr/local/include/python2.7
當我運行的進口你好Python中,它給了我這個錯誤。
>>> import hello
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: ./hello.so: only ET_DYN and ET_EXEC can be loaded
有人可以告訴我什麼是錯?
(我用我的anaconda2主目錄爲Python環境下,但因爲我的深度學習代碼生成OK與升壓蟒蛇,應該是包括在我的系統目錄boost/python.hpp沒問題)