Search code examples
pythonc++python-3.xboostboost-python

Complie Boost.Python code error: library not found for -lpython3.6m


I have an example code of Boost.Python

#include <boost/python.hpp>

char const* greet()
{
    return "hello, world";
}

BOOST_PYTHON_MODULE(hello_ext)
        {
                using namespace boost::python;
                def("greet", greet);
        }

And I compiled it with specifying Python3

g++ -o hello_ext.so -O2 hello.cpp -std=c++11 -fPIC -shared \
-Wall -Wextra `python3.6m-config --includes --libs` \ 
-lboost_python3

But I got the error

ld: library not found for -lpython3.6m
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Python: Python3.6 on /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 Boost.Python: Installed using brew install boost-python --with-python3

The output of python3.6m-config --includes --libs is

-I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m -I/Library/Frameworks/Python.framework/Versions/3.6/include/python3.6m
-lpython3.6m -ldl -framework CoreFoundation

How can I compile my code by specifying python3 and boost.python3?


Solution

  • Check the directory provided by python3.6m-config --ldflags, make sure libpython3.6m.dylib is there. If so, add the options for the above command to your compiler invocation.

    If it's not there, you need to build Python with --enable-shared.