I decided to make my program compatible with windows environment.But I have very little programming experience on windows.There are some errors need help.
Environment:
- os: win7-64bit,
- ide: codeblocks12.11,
- python: Python 2.7.3 Windows X86-64 Installer (Windows AMD64 / Intel 64 / X86-64 binary [1] -- does not include source)
- compiler: mingw that come from codeblocks installation package.
- boost: boost1.52
I only copy and test this "hello" code that come from ".\boost_1_52_0\libs\python\example\tutorial"
Code:
#include <iostream>
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
char const* greet()
{ return "hello, world"; }
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
using namespace std;
int main()
{
cout << "Hello world!" << endl;
return 0;
}
Error info:
mingw32-g++.exe -Wall -fexceptions -g -ID:\boost\include\boost-1_52 -IC:\Python27\include -c E:\project\snail-MW\test1\main.cpp -o obj\Debug\main.o
mingw32-g++.exe -LD:\boost\lib\ -LC:\Python27\libs -o bin\Debug\test1.exe obj\Debug\main.o
obj\Debug\main.o: In function `inithello_ext':
E:/project/snail-MW/test1/main.cpp:11: undefined reference to `boost::python::detail::init_module(char const*, void (*)())'
obj\Debug\main.o: In function `ZNK5boost6python9type_info4nameEv':
D:/boost/include/boost-1_52/boost/python/type_id.hpp:165: undefined reference to `boost::python::detail::gcc_demangle(char const*)'
obj\Debug\main.o: In function `ZNK5boost6python15to_python_valueIRKPKcEclES5_':
D:/boost/include/boost-1_52/boost/python/converter/builtin_converters.hpp:161: undefined reference to `boost::python::converter::do_return_to_python(char const*)'
................
It have also do some errors that only compile by the command of "bjam toolset=gcc variant=release " in the station ".\boost_1_52_0\libs\python\example\tutorial".
You got the paths, but you're not linking against the boost and python library:
Also you're trying to create an executable (that's why you had to add a
main()
to the example in order to compile it). For python modules, you want to create a shared library, matching the name defined by theBOOST_PYTHON_MODULE(...)
macro. The extension of these modules should be.pyd
.If the linker can't find the boost_python library, check your boost library directory. Depending on your installation, you should have a
libboost_python.a
orlibboost_python-mgw??-mt-1_??.a
. If you can't find anything like that, you have to build them first. You should decide if you want to build static or shared libraries. More details here.This will fail if boost can't find your python installation. To configure (usually a non-standard location of) python, edit the
user-config.jam
. This may be in your%HOMEDRIVE%%HOMEPATH%
orboost_1_52_0\tools\build\v2
. LocatePython configuration
in that file and set the proper paths (if python was installed at the default location, this step shouldn't be required). The syntax is:For example:
If you choose to build the static boost python library, you may get other errors when linking your program. If that's the case, you have to indicate that you want to link against the static library with:
Lastly, the compiler may complain about
::hypot
not being declared. To fix that,#include <cmath>
before including the boost/python headers.To make the tutorial run on Boost 1_60_0, on Windows 7, with a 64 bit mingw gcc (version 4.8.1), I needed to use the commands in an msys shell:
to make the boost python library. This command shall be executed in the Boost root directory. Note that without the
define=MS_WIN64
I get undefined errors on__imp_py_InitModule4
as indicated in Python extensions for Win64 via GCC.After that, it is straightforward to compile the Python boost tutorial:
This also worked for Boost 1_54_0