【问题标题】:Boost.Python fails with static librariesBoost.Python 因静态库而失败
【发布时间】:2018-08-12 01:46:56
【问题描述】:

我似乎无法让 Boost.Python 中的简单 hello.cpp 示例工作。

你好.cpp:

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

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

我将 cmake 与 mingw-w64 一起使用,但我不完全确定使用静态和共享 boost 库之间的区别。如果扩展与共享库链接,扩展库是否也必须与 boost 库一起分发?我只想分发一个 dll。 Boost_USE_STATIC_LIBSBoost_USE_STATIC_RUNTIME 有什么区别?如果这些标志中的任何一个设置为ON,则后续编译将失败并出现未定义的符号。如果它们都设置为 OFF,则会创建一个名为 libhello_ext.dll 的文件。但是,我无法让 python 与 import hello_extimport libhello_ext 一起使用它。

这是我的 CMakeLists.txt 文件:

cmake_minimum_required(VERSION 3.12)
project(hello VERSION 1.0)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -Wall -Wextra -pedantic -g")

set(BOOST_LIBRARYDIR C:/boost_1_67_0/stage/x64/lib)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME ON)
find_package(Boost 1.67.0 REQUIRED python37)
if (Boost_FOUND)
    message(" Boost include directory found at ${Boost_INCLUDE_DIRS}")
    message(" Boost libraries found at ${Boost_LIBRARIES}")
else()
    message(" Boost not found")
    return()
endif()

find_package(PythonLibs 3.7 REQUIRED)
if (PYTHONLIBS_FOUND)
    message(" Python include directory found at ${PYTHON_INCLUDE_DIRS}")
    message(" Python libraries found at ${PYTHON_LIBRARIES}")
else()
    message(" Python not found")
endif()

set(SOURCE_FILES hello.cpp)
add_library(hello_ext SHARED ${SOURCE_FILES})

target_include_directories(hello_ext SYSTEM PRIVATE  ${Boost_INCLUDE_DIRS} ${PYTHON_INCLUDE_DIRS})
target_link_libraries(hello_ext ${Boost_LIBRARIES} ${PYTHON_LIBRARIES})

这是 mingw32-make 的输出:

[ 50%] Linking CXX shared library libhello_ext.dll
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `PyInit_hello_ext':
C:/boost_1_67_0/libs/python/example/tutorial/hello.cpp:15: undefined reference to `__imp__ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE'
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `boost::python::type_info::name() const':
C:/boost_1_67_0/boost/python/type_id.hpp:160: undefined reference to `__imp__ZN5boost6python6detail12gcc_demangleEPKc'
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `boost::python::to_python_value<char const* const&>::operator()(char const* const&) const':
C:/boost_1_67_0/boost/python/converter/builtin_converters.hpp:157: undefined reference to `__imp__ZN5boost6python9converter19do_return_to_pythonEPKc'
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `void boost::python::def<char const* (*)()>(char const*, char const* (*)())':
C:/boost_1_67_0/boost/python/def.hpp:91: undefined reference to `__imp__ZN5boost6python6detail17scope_setattr_docEPKcRKNS0_3api6objectES3_'
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `boost::python::api::object boost::python::detail::make_function_aux<char const* (*)(), boost::python::default_call_policies, boost::mpl::vector1<char const*> >(char const* (*)(), boost::python::default_call_policies const&, boost::mpl::vector1<char const*> const&)':
C:/boost_1_67_0/boost/python/make_function.hpp:38: undefined reference to `__imp__ZN5boost6python7objects15function_objectERKNS1_11py_functionE'
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `boost::python::objects::py_function_impl_base::py_function_impl_base()':
C:/boost_1_67_0/boost/python/object/py_function.hpp:20: undefined reference to `__imp__ZTVN5boost6python7objects21py_function_impl_baseE'
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `boost::python::objects::caller_py_function_impl<boost::python::detail::caller<char const* (*)(), boost::python::default_call_policies, boost::mpl::vector1<char const*> > >::~caller_py_function_impl()':
C:/boost_1_67_0/boost/python/object/py_function.hpp:30: undefined reference to `__imp__ZN5boost6python7objects21py_function_impl_baseD2Ev'
CMakeFiles\hello_ext.dir/objects.a(hello.cpp.obj): In function `boost::python::converter::expected_pytype_for_arg<char const*>::get_pytype()':
C:/boost_1_67_0/boost/python/converter/pytype_function.hpp:67: undefined reference to `__imp__ZN5boost6python9converter8registry5queryENS0_9type_infoE'
C:/boost_1_67_0/boost/python/converter/pytype_function.hpp:70: undefined reference to `__imp__ZNK5boost6python9converter12registration25expected_from_python_typeEv'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [CMakeFiles\hello_ext.dir\build.make:88: libhello_ext.dll] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:72: CMakeFiles/hello_ext.dir/all] Error 2
mingw32-make: *** [Makefile:83: all] Error 2

【问题讨论】:

  • 您好!你找到解决这个问题的方法了吗?我刚刚遇到它,找不到问题...

标签: python c++ boost boost-python


【解决方案1】:

出于某种奇怪的原因,必须为 Windows 中的 Boost Python(而不是在 Linux 或 MacOSX 中)强制使用命名约定。

综上,需要补充:

add_definitions(/DBOOST_PYTHON_STATIC_LIB)

到您的 CMake 配置文件。否则,您的链接器将尝试使用 boost_python37-vc141-mt-x64-1_70.lib(与 DLL 关联的导入库,boost_python37-vc141-mt-x64-1_70.dll)而不是 libboost_python37-vc141-mt-x64-1_70.lib,这是“真正的”静态库(愚蠢的 Windows 及其使用相同扩展名的奇怪约定用于导入库和静态库)。

【讨论】:

    猜你喜欢
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 2018-05-16
    • 1970-01-01
    相关资源
    最近更新 更多