hello world python extension in c++ using boost?

社会主义新天地 提交于 2019-12-10 13:25:56

问题


Here's my simple first attempt at a python extension using boost. Can someone help me to understand what's causing the compilation error?


#include <iostream>
using namespace std; 
void say_hello(const char* name) {
    cout << "Hello " <<  name << "!\n";
}

#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    def("say_hello", say_hello);
}

user@host:~$g++ main.cpp -o test.so

In file included from /usr/include/boost/python/detail/prefix.hpp:13:0, from /usr/include/boost/python/module.hpp:8, from main.cpp:8: /usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory compilation terminated.

回答1:


/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: No such file or directory compilation terminated.

This line tells exactly why it doesn't work. Your compiler doesn't know where is the pyconfig.h file. You have two options here:

  1. place pyconfig.h in a location that g++ knows about (i.e. your project's directory)
  2. add -I DIRECTORY (this is capital i, not lowercase L) flag to g++ that will make g++ search DIRECTORY for header files

g++ -I /path/to/my/include/files main.cpp




回答2:


If you face this problem in your NetBeans then just add "/usr/include/python 2.7/" folder in your NetBeans additional include option. You will get this additional include option in properties.




回答3:


You need to place pyconfig.h in same directory



来源:https://stackoverflow.com/questions/6007185/hello-world-python-extension-in-c-using-boost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!