【问题标题】:ld: library not found for -lboost_python on MacOSld:在 MacOS 上找不到 -lboost_python 的库
【发布时间】:2019-01-25 03:04:57
【问题描述】:

在 Mac 上,我想构建示例 Boost.Python 代码

hello.cpp

#include <boost/python.hpp>

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

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

我用brew install boost-python --with-python3安装了Boost.Python

我编译了hello.cpp by

g++ -fpic -c -L/usr/local/Cellar/boost/1.67.0_1/lib `python3.6m-config --includes --libs  --ldflags`  hello.cpp  

生成了一个hello.o 文件。并通过

生成.so文件
g++ -shared -L/usr/lib -L/usr/local/Cellar/boost/1.67.0_1/lib -L/usr/local/Cellar/boost-python3/1.67.0_1/lib/  `python3.6m-config --libs --ldflags` -lboost_python3 -o hello_ext.so hello.o

但它返回了

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

或更详细的输出使用-v

g++ -shared -L/usr/lib -L/usr/local/Cellar/boost/1.67.0_1/lib -L/usr/local/Cellar/boost-python3/1.67.0_1/lib/  `python3.6m-config --libs --ldflags`  -lboost_python3 -o hello_ext.so hello.o -v
Apple LLVM version 9.0.0 (clang-900.0.38)
Target: x86_64-apple-darwin17.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
 "/Library/Developer/CommandLineTools/usr/bin/ld" -demangle -lto_library /Library/Developer/CommandLineTools/usr/lib/libLTO.dylib -dynamic -dylib -arch x86_64 -macosx_version_min 10.13.0 -o hello_ext.so -L/usr/lib -L/usr/local/Cellar/boost/1.67.0_1/lib -L/usr/local/Cellar/boost-python3/1.67.0_1/lib/ -L/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin -lpython3.6m -ldl -framework CoreFoundation -lpython3.6m -ldl -framework CoreFoundation -lboost_python3 hello.o -lc++ -lSystem /Library/Developer/CommandLineTools/usr/lib/clang/9.0.0/lib/darwin/libclang_rt.osx.a
ld: library not found for -lboost_python3
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我该如何解决?

【问题讨论】:

    标签: python c++ boost homebrew boost-python


    【解决方案1】:

    最后发现boost-python lib路径中没有libboost_python37.dylib这个

    $ ls /usr/local/Cellar/boost-python3/1.67.0_1/lib
    libboost_numpy37-mt.a      libboost_numpy37.a         libboost_python37-mt.dylib libboost_python37.dylib
    libboost_numpy37-mt.dylib  libboost_numpy37.dylib     libboost_python37-mt.a     libboost_python37.a
    

    所以我创建了一个软链接

    $ sudo ln -s libboost_python37.dylib libboost_python3.dylib
    

    我现在可以编译我的代码了。

    【讨论】:

    • 或者你可以使用-lboost_python37来代替
    • @leiyc,是的,-lboost_python37 是另一种选择。
    猜你喜欢
    • 1970-01-01
    • 2021-08-24
    • 1970-01-01
    • 2018-06-25
    • 2021-06-07
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    相关资源
    最近更新 更多