Search code examples
gccg++macportsboost-python.so

Making .so from .cpp: Undefined symbols for architecture x86_64: Boost.Python MacPorts GCC6 not Clang


I am attempting to follow the Exposing Classes tutorial for Boost.Python at Boost.org. I already did the first tutorial successfully and when commenting out the code, the error occurs as soon as the class appears. The part where everything falls apart is in the making of the .so file from the .cpp file. I get the apparently common : 'Undefined symbols for architecture x86_64:'. I have checked multiple questions on this subject and all had essentially nothing to do with my case. I am on OSX 10.11.6 and I have used macports to install boost, and gcc6. I have set everything up so I actually use gcc and g++ instead of clang. This is why I don't believe my problem is related to this one because I am using actual g++, I should not be dealing with this libstc++ and libc++ problem, if i am correct in my understanding. All of my packages from macports, and macports itself are up-to-date. Anybody care to share some insight?

When I run the command to create the .so: (I believe I must be missing some link to a lib in Boost, but I have no idea what it is)

g++ -shared -o hello.so -fPIC hello.cpp `python-config --cflags --libs` -lboost_python

I get a little warning and a short error message, but the hello.so.dSYM file is created and saved in directory:

cc1plus: warning: command line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++

Undefined symbols for architecture x86_64: "boost::python::objects::function_object(boost::python::objects::py_function const&, std::pair const&)", referenced from:

    init_module_hello()     in cchDzx8t.o

"boost::python::objects::register_dynamic_id_aux(boost::python::type_info, std::pair ()(void))", referenced from:

    init_module_hello()     in cchDzx8t.o

ld: symbol(s) not found for architecture x86_64

collect2: error: ld returned 1 exit status

My .cpp code is exactly the same as shown in that tutorial:

#include <boost/python.hpp>
using namespace boost::python;
struct World {   
    void set(std::string msg) { this->msg = msg; }
         std::string greet() { return msg; }
         std::string msg;
};
BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
    .def("greet", &World::greet)
    .def("set", &World::set)
    ;   
}

Solution

  • Regardless of whether you have GCC/G++ installed correctly, MacPorts always builds its C++ software against libc++ on systems where clang would compile with libc++ as default.

    That means that your MacPorts version of Boost is compiled with libc++, which means you cannot use it with GCC/G++ (unless you jump through some extra hoops to use libc++ with GCC).

    Despite your guess that your problem is not what you described in the other post you linked, it is exactly that. Your options are:

    • Use clang++
    • Build your own copy of Boost with g++