0

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)

maheshakya
  • 2,198
  • 7
  • 28
  • 43
  • Don't do `using namespace std;` before the header includes - this could cause naming conflicts within the headers. If you want it for your own purposes, place it after the last include. – Nathan Ernst May 16 '13 at 20:15

1 Answers1

0

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).
Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55