【问题标题】:Boost.Python and CMake Link and Load ErrorsBoost.Python 和 CMake 链接和加载错误
【发布时间】:2011-05-01 20:06:28
【问题描述】:

我有一个像这样的main.cpp

#include <boost/python.hpp>

const char* greeting()
{
    return "Hello world?";
}

BOOST_PYTHON_MODULE(test)
{
    using namespace boost::python;

    def("greeting", greeting);
}

还有一个CMakeLists.txt 文件:

project(test)
cmake_minimum_required(VERSION 2.8)

# get boost
set(Boost_USE_STATIC_LIBS   ON)
set(Boost_USE_MULTITHREADED ON)
find_package(Boost COMPONENTS
                system
                thread
                python
             REQUIRED)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

# get python
find_package(PythonLibs REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
link_directories(${PYTHON_LIBRARIES})

add_library(test SHARED
        main.cpp
    )

我可以运行 cmakemake 就好了。它为我输出了一个不错的 libtest.so 小文件。为了测试它,我有一个这样的 Python 脚本:

import libtest

print(libtest.greeting())

在与libtest.so 相同的目录中运行它会出现以下错误:

Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import libtest
ImportError: /home/travis/projects/boost-python-test/build/libtest.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

哎呀! make VERBOSE=1 的问题很明显......创建我的libtest.so 的行如下所示:

/usr/bin/c++  -fPIC   -shared -Wl,-soname,libtest.so -o libtest.so CMakeFiles/test.dir/main.cpp.o -L/usr/lib/libpython2.7.so

我有一个心理障碍,为什么我在那条线上看不到-L/usr/lib/libboost_python-mt-py27.a。它显然适用于find_package(PythonLibs ...)。由于一些 CMake 的新奇,我做得不够好。

【问题讨论】:

    标签: cmake boost-python


    【解决方案1】:

    解决这个问题的方法很简单。必须在add_library 语句之后用target_link_libraries 显式链接库。

    target_link_libraries(test
            ${Boost_LIBRARIES}
            ${PYTHON_LIBRARIES}
        )
    

    我仍然不确定为什么它在没有它的情况下适用于 Python。魔法?

    【讨论】:

    • 感谢您在自己找到答案后发布答案,这解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 2023-01-27
    相关资源
    最近更新 更多