编译一些代码与boost.python通过mingw在win7-64bit [英] compile some code with boost.python by mingw in win7-64bit

查看:759
本文介绍了编译一些代码与boost.python通过mingw在win7-64bit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定让我的程序与Windows环境兼容。但是我在windows上有很少的编程经验。有一些错误需要帮助。



环境: p>


  • os:win7-64bit,

  • ide:codeblocks12.11,

  • python:Python 2.7.3 Windows X86-64安装程序(Windows AMD64 / Intel
    64 / X86-64二进制文件[1] - 不包括源代码)

  • 编译器:mingw来自代码块安装包。

  • boost:boost1.52



我只复制并测试来自.\boost_1_52_0\libs\python\example\ tutorial的此hello代码



代码:

  #include< iostream> 
#include< boost / python / module.hpp>
#include< boost / python / def.hpp>

char const * greet()
{returnhello,world; }

BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost :: python;
def(greet,greet);
}

using namespace std;
int main()
{
cout<< 你好,世界! << endl
return 0;
}

错误信息:

  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:在函数`inithello_ext'中:
E :/project/snail-MW/test1/main.cpp:11:未定义的引用`boost :: python :: detail :: init_module(char const *,void(*)())'
obj \\ Debug \main.o:在函数`ZNK5boost6python9type_info4nameEv':
D:/boost/include/boost-1_52/boost/python/type_id.hpp:165:undefined对`boost :: python :: detail: :gcc_demangle(char const *)'
obj \Debug\main.o:在函数`ZNK5boost6python15to_python_valueIRKPKcEclES5_'中:
D:/ boost / include / boost-1_52 / boost / python / converter / builtin_converters .hpp:161:未定义引用`boost :: python :: converter :: do_return_to_python(char const *)'
................

它也做一些错误,只能通过命令bjam toolset = gcc variant = 。\boost_1_52_0\libs\python\example\tutorial。

解决方案

你有路径, 're not linked with boost and python library:

  -lboost_python -lpython27 

此外,你正在尝试创建一个可执行文件(这就是为什么你必须添加一个 main()到例子为了编译它)。
对于python模块,您要创建一个共享库,它匹配由 BOOST_PYTHON_MODULE(...)宏定义的名称。
这些模块的扩展名应为 .pyd

  -shared -o hello_ext.pyd 

如果链接器找不到boost_python库, boost库目录。根据你的安装,你应该有一个
libboost_python.a libboost_python-mgw ?? - mt-1 _ ??。 / code>。如果你找不到这样的东西,你必须先构建它们。
您应该决定是否要构建静态或共享库。详情请这里

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



如果boost无法找到你的python安装。要配置(通常是非标准位置)的python,
编辑 user-config.jam 。这可能在您的%HOMEDRIVE %% HOMEPATH% boost_1_52_0\tools\build\v2 中。
在该文件中找到 Python配置并设置正确的路径(如果python安装在默认位置,则不需要此步骤)。
语法是:

 使用python:python_version:python_base:python_incl:python_lib; 

例如:

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

如果选择构建静态boost python库,则在链接程序时可能会遇到其他错误。如果是这种情况,您必须指明您要使用以下链接来链接静态库:

  -DBOOST_PYTHON_STATIC_LIB 

最后,编译器可能会抱怨 :: hypot 宣布。要解决这个问题, #include< cmath> 之前包含boost / python头。


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:

-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 <cmath> before including the boost/python headers.

这篇关于编译一些代码与boost.python通过mingw在win7-64bit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆