Search code examples
pythonc++gccboost-python

Undefined symbols for architecture x86_64: "_PyUnicode_Type", referenced from: when using python_boost


My environment is using Anaconda/Python 3.7.9 on Mac OSX Catalina.

I have installed boost_python for conda boost

#include <boost/python.hpp>

char const * doYouDo( const char* jobs ){
    return "Hello, I am an embedded engineer.";
}

BOOST_PYTHON_MODULE( what ){
    boost::python::def( "doYouDo", doYouDo);
}

then compile with this.

g++ -fPIC -Wall -I/Users/whitebear/anaconda3/envs/aiwave/include/boost/python/ -I/Users/whitebear/anaconda3/envs/aiwave/include/python3.7m/ -lboost_python37 -shared -o whatModule.so what.cpp

I got this error below.

In file included from what.cpp:1:
In file included from /usr/local/include/boost/python.hpp:28:
In file included from /usr/local/include/boost/python/exception_translator.hpp:10:
/usr/local/include/boost/bind.hpp:36:1: warning: The practice of declaring the Bind placeholders (_1, _2, ...) in the global namespace is deprecated. Please use
      <boost/bind/bind.hpp> + using namespace boost::placeholders, or define BOOST_BIND_GLOBAL_PLACEHOLDERS to retain the current behavior. [-W#pragma-messages]
BOOST_PRAGMA_MESSAGE(
^
/usr/local/include/boost/config/pragma_message.hpp:24:34: note: expanded from macro 'BOOST_PRAGMA_MESSAGE'
# define BOOST_PRAGMA_MESSAGE(x) _Pragma(BOOST_STRINGIZE(message(x)))
                                 ^
<scratch space>:2:2: note: expanded from here
 message("The practice of declaring the Bind placeholders (_1, _2, ...) " "in the global namespace is deprecated. Please use " "<boost/bind/bind.hpp> + using namespace bo...
 ^
1 warning generated.
Undefined symbols for architecture x86_64:
  "_PyUnicode_Type", referenced from:
      boost::python::to_python_value<char const* const&>::get_pytype() const in what-7927a1.o
  "__Py_NoneStruct", referenced from:
      boost::python::api::object::object() in what-7927a1.o
      boost::python::converter::pointer_arg_from_python<char const*>::pointer_arg_from_python(_object*) in what-7927a1.o
      boost::python::converter::pointer_arg_from_python<char const*>::operator()() const in what-7927a1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I guess it says there is no _PyUnicode_Type,__Py_NoneStruct

However which library has them?? Any suggestion appreciated.


Solution

  • It solved with python-config --ldflags

    My final gcc command is below. aiwave is my anacond env name

    $export LIBRARY_PATH=/Users/whitebear/anaconda3/envs/aiwave/lib/
    $g++ -fPIC -Wall -I/Users/whitebear/anaconda3/envs/aiwave/include/boost/python/ -I/Users/whitebear/anaconda3/envs/aiwave/include/python3.7m/ `python-config --ldflags` -lboost_python37 -shared -o whatModule.so what.cpp
    

    Additional information

    my python-config is 2.7 but it works somehow

    $python-config --ldflags
    -L/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config -lpython2.7 -ldl -framework CoreFoundation