gpt4 book ai didi

python - Boost.Python 因静态库而失败

转载 作者:太空狗 更新时间:2023-10-29 23:11:07 25 4
gpt4 key购买 nike

我似乎无法让 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,则后续编译将失败并出现 undefined symbol 。如果它们都设置为 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

最佳答案

出于某些奇怪的原因,必须在 Windows(不是在 Linux 或 MacOSX 中)中为 Boost Python 强制命名约定(并且仅对于 Boost Python,其他 Boost 模块不需要这个)。

总而言之,您需要添加:

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 及其怪异的约定,对导入库和静态库使用相同的扩展名库)。

关于python - Boost.Python 因静态库而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51805078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com