python源码编译 mingw,编译一些代码与boost.python通过mingw在win7-64bit

博主在尝试使自己的程序兼容Windows环境时,使用Code::Blocks 12.11和Python 2.7.3进行编程,遇到了编译Boost Python模块时的链接错误。错误主要涉及未找到boost_python和python27库的引用。解决方案包括正确链接boost和python库,创建共享库,并配置Boost构建系统以找到Python安装路径。如果需要,还可能需要构建静态或动态库。
摘要由CSDN通过智能技术生成

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

#include

#include

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:

-lboost_python -lpython27

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 the BOOST_PYTHON_MODULE(...) macro.

The extension of these modules should be .pyd.

-shared -o hello_ext.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 or libboost_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.

c:\boost_1_52> b2 toolset=gcc --with-python

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% or boost_1_52_0\tools\build\v2.

Locate Python 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:

using python : python_version : python_base : python_incl : python_lib ;

For example:

using python : 2.7 : c:/Python27 : c:/Python27/include : c:/Python27/libs ;

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:

-DBOOST_PYTHON_STATIC_LIB

Lastly, the compiler may complain about ::hypot not being declared. To fix that, #include before including the boost/python headers.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值