1

I've been trying to extend Python with C++ using Boost in Windows 7. This is the code I have so far:

C++ code, hellomodule.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);
}

Python code, setup.py:

#!/usr/bin/env python

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

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

I try to build this by opening the command prompt and running "python setup.py build" in my Python32 directory. I've included the path to vcvarsall.bat in my environment variables.

The error I'm getting now:

error_image.

I have little experience with C++ and I'm new to Boost. Any help would be appreciated.

EDIT: This is being done in MVSC++ 2010, however I'm using the v9.0 toolset and have also tried in MVSC++ 2008 Express. The path to hellomodule.cpp is "C:\Users\Amir\Documents\Visual Studio 2010\Projects\BoostExample\BoostExample\hellomodule.cpp"

1
  • "hellomodule.cpp': no such file or directory. it means you have wrong path to it Commented Feb 27, 2012 at 22:07

1 Answer 1

1

The compiler can't find hellomodule.cpp because it (probably) isn't in c:\python32. Try moving hellomodule.cpp into c:\python32 -- or, better yet, changing your directory to the path of hellomodule.cpp -- and rebuilding. It may lead to some other compiler errors, but at least you'll get past this first problem.

Good luck.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks for the response; I tried running from the directory the C++ source code was in, and it corrected this error, but as you predicted it lead to other compiler errors. Here's the new output. I definitely have boost/python/module.hpp, it's just a matter of letting the compiler find it. Any ideas? +1 for your help so far by the way. :)
@Tagc Sure, now you need to add the Boost path to the compiler's list of includes. There are a couple of ways to do this, but for now just type the following at your command prompt: set INCLUDE=%INCLUDE%;<path-to-boost-here>. So if boost is in c:\boost, you'd type set INCLUDE=%INCLUDE%;c:\boost. Then on to new errors. ;) [Edit: note that I'm assuming you're still building at the command prompt, and that you're making this change after running vcvarsall.bat]
Thanks again. :) I thought I'd already set the include and library paths in both MVSC++ 2010 and MVSC++ 2008 Express, but doing what you said definitely seemed to help me make progress. Now it seems to be able to find the include files, but it complains about not being able to access the library files, but I'm guessing this would be resolved in a similar way to what you just described.i.imgur.com/rJAAo.png
@Tagc Yep, same process, but append boost's lib path (e.g., c:\boost\stage\lib) to the end of either LIB or LIBPATH using the same process that you did with INCLUDE. I'm less certain about this step since I haven't had to configure a command line build, but the linker knows which lib is missing (boost_python.lib). It's just a matter of getting it to search the folder that it's located in.
Thanks, but I've just been working with cython and it's absolutely wonderful. I don't think I need to work with Boost anymore. I'll declare your answer accepted for all the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.