在 Windows 上使用 Boost python3 和 numpy3 库时,VS2015 中LNK2019未解析的外

LNK2019 Unresolved External Symbol in VS2015 when using Boost python3 and numpy3 libraries on Windows

本文关键字:LNK2019 VS2015 库时 Windows Boost numpy3 python3      更新时间:2023-10-16

>我已经使用以下命令在Windows上下载并构建了Boost 1.64:

b2 --build-type=complete address-model=64 toolset=msvc stage

我在我的主目录中添加了一个user-config.jam文件,告诉Boost在哪里可以找到Python 3:

using python : 3.6 : c:\anaconda3\python ;

然后,我尝试使用 boost::p ython 和 boost::numpy 编译一个小型测试项目:

#include <boost/python.hpp>
#include <boost/python/numpy.hpp>
namespace bp = boost::python;
namespace np = boost::python::numpy;
np::ndarray test_make_zeros(int rows, int cols)
{
    return np::zeros(bp::make_tuple(rows, cols), np::dtype::get_builtin<float>());  
}
BOOST_PYTHON_MODULE(test_boost_numpy)
{
    np::initialize();
    bp::def("test_make_zeros", test_make_zeros);
}

我正在使用以下CMakeLists.txt文件:

cmake_minimum_required(VERSION 3.8)
project(test_boost_python)
set(BOOST_ROOT "C:/Boost-1.64")
SET(Boost_ADDITIONAL_VERSIONS 1.64)
find_package(PythonLibs 3.6 REQUIRED)
include_directories(${PYTHON_INCLUDE_DIRS})
find_package(Boost REQUIRED COMPONENTS python3 numpy3)
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
add_library(test_boost_python SHARED test_boost_python.cpp)
set_target_properties(test_boost_python PROPERTIES PREFIX "" SUFFIX ".pyd")
set_target_properties(test_boost_python PROPERTIES DEFINE_SYMBOL "BOOST_ALL_NO_LIB")
target_link_libraries(test_boost_python ${PYTHON_LIBRARIES} ${Boost_LIBRARIES})

我的 boost::p ython 库被命名为 boost_python3-vc140-mt-1_64.lib,而 boost::numpy 在链接到 Python 3.6 时最终为 boost_numpy3-vc140-mt-1_64.lib。

我不得不打开BOOST_ALL_NO_LIB。如果没有,编译器将查找 boost_python-vc140-mt-1_64.lib boost_numpy-vc140-mt-1_64.lib(名称错误,库名称后缺少数字 3(。(这是Windows上的错误吗?

对于我的测试项目,我现在收到以下链接错误:

test_boost_python.obj : error LNK2019: unresolved external symbol "class boost::python::numpy::dtype __cdecl boost::python::numpy::detail::get_float_dtype<32>(void)" (??$get_float_dtype@$0CA@@detail@numpy@python@boost@@YA?AVdtype@123@XZ) referenced in function "public: static class boost::python::numpy::dtype __cdecl boost::python::numpy::detail::builtin_dtype<float,0>::get(void)" (?get@?$builtin_dtype@M$0A@@detail@numpy@python@boost@@SA?AVdtype@345@XZ)

这个缺失符号的原因可能是什么,我应该如何修复它?

这原来是 Boost::P ython 中的一个错误,请参阅问题 #125 并提交 3844c4。它已在 Boost 的最新开发分支中修复。我相信它会在 1.65 版中修复。