简体   繁体   中英

Error in building boost:python code

I've the following boost:python code(gona.cpp).

#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);
}
int main(){
    return 0;
}

I have boost 1.47(boost pro) installed in my system(Windows 7 32-bit). I used Microsoft Visual Studio 2010 to build this and it has been built successfully. But I want to use this in a python code (as an import). I have the following piece of code (setup.py) to build this into a python module.

from distutils.core import setup
from distutils.extension import Extension

setup(name="PackageName",
    ext_modules=[
        Extension("hello", ["gona.cpp"],
        libraries = ["boost_python", "boost"])
    ])

"gona" is the file C++ file name. I used the following command to build this in the command line.

python setup.py build

After I do this, I get the following error.

>python setup.py build
running build
running build_ext
building 'hello' extension
C:\MinGW\bin\gcc.exe -mdll -O -Wall -IC:\Python27\include -IC:\Python27\PC -c De
cision_Tree.cpp -o build\temp.win32-2.7\Release\gona.o
gona.cpp:9:35: fatal error: boost/python/module.hpp: No such file or di
rectory
compilation terminated.
error: command 'gcc' failed with exit status 1

It seems the boost installation I have in my system works only on Visual studio(as it had been built successfully). I've run other boost programs without problems in Visual studio. How do I build this as a python module, so that it can be imported in python code?. (Using command line or Visual studio)

Several things here:

  • You can also build a Python module using VS2010 directly, just set the output path to foo.pyd instead of foo.dll.
  • setup.py seems to use MinGW, maybe you can convince it to use the working VS2010 setup instead?
  • You can use "-I" to inform GCC about include paths, see the GCC documentation. I'm not sure how to tell setup.py where it should look for include paths though, but you should be able to locate that documentation easily.
  • It looks like setup.py is trying to compile as C code ("gcc.exe" instead of "g++.exe"), which could cause further problems.
  • Your code has a main() function, which is not required in a DLL (binary Python modules are effectively DLLs).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM