pybind 与 Windows 上的第三方 C++导入错误:DLL 加载失败

问题描述

我正在尝试使用 pybind11 使用来自 python 的第三方 C++ 库。

我已设法使用纯 C++ 代码完成此操作,但是,当我添加第三方代码时,出现以下错误

ImportError: DLL load Failed while importing recfusionbind

这是我的代码

#include <RecFusion.h>
#include <pybind11/pybind11.h>

using namespace RecFusion;

bool isValidLicense() {
    return RecFusionSDK::setLicenseFile("License.dat");
}

namespace py = pybind11;

PYBIND11_MODULE(recfusionbind,m) {
    m.def("isValidLicense",&isValidLicense,R"pbdoc(
        Check if there is a valid license file.
    )pbdoc");

#ifdef VERSION_INFO
    m.attr("__version__") = VERSION_INFO;
#else
    m.attr("__version__") = "dev";
#endif
}

如果我出于测试目的更改函数返回到简单的 true 它工作得很好。但是调用第三方库会出现上述错误。我已将 RecFusion.dll 放在当前目录(python 脚本所在的目录)中,结果相同。

欢迎任何关于我遗漏的提示。 Lib 是 64 位的,和我的代码一样。

解决方法

在我看来,这像是一个链接问题。查看您的 dll 的导入表(使用 PE 资源管理器或 IDA 等工具)。现在验证函数 RecFusionSDK::setLicenseFile 确实是从名为 RecFusion.dll 的 dll 而不是其他某个 dll 名称导入的。

还要验证 RecFusion.dll 的导出表和您的 dll 中的重整函数名称是否匹配。如果没有,这可能是 ABI 问题。