我似乎无法使Boost.Python中的简单hello.cpp示例起作用。
hello.cpp:
char const* greet()
{
return "hello, world";
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
我在mingw-w64上使用cmake,并且不确定使用静态和共享Boost库之间的区别。如果扩展与共享库链接,扩展库是否也必须与boost库一起分发?我只想分发一个dll。 Boost_USE_STATIC_LIBS
和Boost_USE_STATIC_RUNTIME
有什么区别?如果这些标志之一设置为ON
,则后续编译将失败,并带有未定义的符号。如果将它们都设置为OFF,则会创建一个名为libhello_ext.dll的文件。但是,我无法让python在import hello_ext
或import 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
答案 0 :(得分:0)
出于某种奇怪的原因,必须在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及其怪异的库)。惯例,以便对导入库和静态库使用相同的扩展名。