我在这里看看如何向Python公开c ++.我已经构建了Python深度学习代码,它使用boost-python连接c ++和python,它运行正常,所以我的系统有boost-python alread设置的东西.这里是我的HELLO.CPP代码(我曾经WorldC和WorldP清楚地显示在声明中C++和Python的类名称的使用.我不知道为什么原始网页使用相同的类名World引起混乱初学者. )
#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)
;
}
Run Code Online (Sandbox Code Playgroud)
这就是我打招呼的方式.所以
g++ -shared -c -o hello.so -fPIC hello.cpp -lboostpython -lpython2.7 -I/usr/local/include/python2.7
Run Code Online (Sandbox Code Playgroud)
当我在python中运行import hello时,它给了我这个错误.
>>> 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
Run Code Online (Sandbox Code Playgroud)
谁能告诉我什么是错的?
(我用我的anaconda2主目录为Python环境下,但因为我的深度学习代码生成OK与升压蟒蛇,应该是包括在我的系统目录boost/python.hpp没问题)
Cha*_*Kim 10
我早就忘记了这个问题,今天又重新回答了这个问题.
我发现了两个问题.第一个问题是我给了-c选项,它使编译器只编译源而不是链接.第二个问题是库名称错误(我搜索/ usr/lib64并且有libboost_python.so,所以它应该是-lboost_python而不是-lboostpython).所以构建它的正确方法是:
g++ -shared -o hello.so -fPIC hello.cpp -lboost_python -lpython2.7 -I/usr/local/include/python2.7
Run Code Online (Sandbox Code Playgroud)
我发现它在python中运行正常.:)
| 归档时间: |
|
| 查看次数: |
3023 次 |
| 最近记录: |