简体   繁体   中英

Linking error with boost::python

I have a problem getting started with Boost's python library. My code is

#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;
}

I tried a lot and the closest I get to a working program is a linking error with 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

I installed Boost through apt-get (libboost-all-dev) so gcc should find it, shouldn't it? I know that bjam is the preferred way to compile programs using boost::python, but as i just wanted the python interpreter for plotting purposes, i don't wanted to start up with Jamfile and boost-build.jam and so on. So what do I have to do to link against the library?

As Praetorian say you need to link with the proper library.

Add -lboost_python to the command line:

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

Unfortunately the boost-python documentation isn't very clear on the subject, but there is a general instruction for boost on how to link with their libraries: how to link to a boost library .

Linking is necessary whenever you use a library that isn't header only.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM