【问题标题】:Linking error with boost::python与 boost::python 链接错误
【发布时间】:2015-05-07 18:18:57
【问题描述】:

我在开始使用 Boost 的 python 库时遇到了问题。我的代码是

#include <boost/python.hpp>
#include <Python.h>

namespace python=boost::python;

int main(int argc, char const *argv[])
{
    Py_Initialize();

    python::dict global;

    return 0;
}

我尝试了很多,最接近工作程序的是 boost 的链接错误:

$ gcc -c $(python2.7-config --cflags)  bpt.cpp            
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
$ gcc bpt.o $(python2.7-config --ldflags) -o bpt
bpt.o: In Funktion `dict':
/usr/include/boost/python/dict.hpp:89: Nicht definierter Verweis auf `boost::python::detail::dict_base::dict_base()'
collect2: error: ld returned 1 exit status

我通过 apt-get (libboost-all-dev) 安装了 Boost,所以 gcc 应该可以找到它,不是吗?我知道 bjam 是使用 boost::python 编译程序的首选方式,但由于我只是想要 python 解释器用于绘图,我不想启动 Jamfile 和 boost-build.jam 等等。那么我需要做什么才能链接到图书馆呢?

【问题讨论】:

  • Boost.Python 不仅仅是标题,您还需要链接到库。尝试将-lboost_python 添加到 gcc 命令行。
  • 谢谢,那是我丢失的标志。你能说明一下我自己在哪里可以找到的吗?
  • 据我所知,CMake 也可以为您做到这一点。见stackoverflow.com/a/5238644/147808
  • @Praetorian 为什么你的答案在评论中?
  • 它应该列在 Boost.Python 文档的某个地方(希望 Boost.Python 在文档方面不是最好的)

标签: c++ boost


【解决方案1】:

正如 Praetorian 所说,您需要链接到适当的库。

在命令行中添加-lboost_python

gcc bpt.o -lboost_python $(python2.7-config --ldflags) -o bpt

不幸的是,boost-python 文档在这个主题上不是很清楚,但是有一个关于如何与他们的库链接的一般说明:how to link to a boost library

只要您使用的库不仅仅是标题,就需要链接。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-10
    • 1970-01-01
    • 2014-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    相关资源
    最近更新 更多