murrayc
murrayc

Reputation: 2123

Linker error with libboost_python-py34 on Ubuntu 15.04

I must be making some simple mistake trying to use boost-python on Ubuntu Linux 15.04 (Vivid Vervet), with the libboost-python1.55-dev package installed.

I'm trying to build a simple test file like so:

$ g++ -o conftest -Wall -I/usr/include/python3.4m -I/usr/include/x86_64-linux-gnu/python3.4m  -L/usr/lib/x86_64-linux-gnu -lboost_python-py34 -lpython3.4m conftest.cc

But that gives me this linker error:

/tmp/ccxkW5XR.o: In function `PyInit_test':
conftest.cc:(.text+0x7e): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())'
collect2: error: ld returned 1 exit status

But that symbol really does seem to exist:

$ nm -D --demangle /usr/lib/x86_64-linux-gnu/libboost_python-py34.so | grep "init_module"
0000000000033ac0 T boost::python::detail::init_module(PyModuleDef&, void (*)())

This is the very simple test code that I'm building, based on a config test used by the AX_BOOST_PYTHON autoconf macro:

#include <boost/python/module.hpp>

BOOST_PYTHON_MODULE(test) { throw "Boost::Python test."; }

int
main ()
{
  return 0;
}

Can anyone see what I'm doing wrong?

Upvotes: 1

Views: 1110

Answers (1)

James Henstridge
James Henstridge

Reputation: 43899

The first thing that stuck out to me in your compiler invocation is that you've listed the libraries before the source file that uses them on the command line.

This will usually work anyway with shared libraries, but isn't strictly correct. For instance, it'll break if you're using static libraris, since only the objects from the archive needed to satisfy symbols from things earlier in the command line are included. But it seems to make a difference here, even though we're using shared libraries:

$ g++ -o conftest -Wall -I/usr/include/python3.4m -I/usr/include/x86_64-linux-gnu/python3.4m  -L/usr/lib/x86_64-linux-gnu -lboost_python-py34 -lpython3.4m conftest.cc
/tmp/ccj8Znlk.o: In function `PyInit_test':
conftest.cc:(.text+0x7e): undefined reference to `boost::python::detail::init_module(PyModuleDef&, void (*)())'
collect2: error: ld returned 1 exit status
$ g++ -o conftest -Wall -I/usr/include/python3.4m -I/usr/include/x86_64-linux-gnu/python3.4m  -L/usr/lib/x86_64-linux-gnu conftest.cc -lboost_python-py34 -lpython3.4m
$ ldd conftest | grep python
    libboost_python-py34.so.1.55.0 => /usr/lib/x86_64-linux-gnu/libboost_python-py34.so.1.55.0 (0x00007f6291003000)
    libpython3.4m.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython3.4m.so.1.0 (0x00007f62909c2000)

The second g++ invocation succeeds and produces a correctly linked executable.

Upvotes: 1

Related Questions