Python中的C ++扩展'DLL加载失败'

问题描述 投票:0回答:1

对于一个学校项目,我需要将Intel Realsense(3D摄像机)与Python连接起来,才能与RoboDK和OpenCV一起使用。我正在使用MVS 2019进行此操作。将superfastcode2(C ++)和RealsenseDistanceV3(Python)设置为64位。

此教程是我创建C ++扩展所遵循的,并且效果很好。

现在我将<librealsense2\rs.hpp>包含在module.cpp代码中,所有内容均可编译并正常运行。但是,在发布模式下为[[only。在调试模式下,编译时出现以下错误:

Error LNK2019 unresolved external symbol __imp__invalid_parameter referenced in function "void * __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)" (??$_Allocate_manually_vector_aligned@U_Default_allocate_traits@std@@@std@@YAPEAX_K@Z) superfastcode2 C:\Users\Gebruiker\source\repos\RealsenseDistanceV3\superfastcode2\module.obj Error LNK2019 unresolved external symbol __imp__CrtDbgReport referenced in function "void * __cdecl std::_Allocate_manually_vector_aligned<struct std::_Default_allocate_traits>(unsigned __int64)" (??$_Allocate_manually_vector_aligned@U_Default_allocate_traits@std@@@std@@YAPEAX_K@Z) superfastcode2 C:\Users\Gebruiker\source\repos\RealsenseDistanceV3\superfastcode2\module.obj
当使用C ++代码中的Realsense库中的函数(rs2::pipeline p;)时,会发生以下错误。 

导入superfastcode2时DLL加载失败:Kan opgegeven模块niet vinden(英语:找不到模块)。]] >>这是合乎逻辑的,因为当查看superfastcode2.log时它说:

Creating library C:\Users\Gebruiker\source\repos\RealsenseDistanceV3\Release\superfastcode2.lib and object C:\Users\Gebruiker\source\repos\RealsenseDistanceV3\Release\superfastcode2.exp module.obj : error LNK2001: unresolved external symbol __imp__PyFloat_AsDouble module.obj : error LNK2001: unresolved external symbol __imp__PyFloat_FromDouble module.obj : error LNK2001: unresolved external symbol __imp__PyModule_Create2 C:\Users\Gebruiker\source\repos\RealsenseDistanceV3\Release\superfastcode2.pyd : fatal error LNK1120: 3 unresolved

Module.cpp(来自superfastcode2):

#include <Python.h> #include <Windows.h> #include <cmath> #include <librealsense2\rs.hpp> // Include RealSense Cross Platform API #include <iostream> // for cout const double e = 2.7182818284590452353602874713527; double sinh_impl(double x) { return (1 - pow(e, (-2 * x))) / (2 * pow(e, -x)); } double cosh_impl(double x) { return (1 + pow(e, (-2 * x))) / (2 * pow(e, -x)); } double askRealsenseDistance() { return 5.25; } void connectRealsense() { rs2::pipeline p; } PyObject* tanh_impl(PyObject*, PyObject* o) { double x = PyFloat_AsDouble(o); double tanh_x = sinh_impl(x) / cosh_impl(x); return PyFloat_FromDouble(tanh_x); } PyObject* askRealsenseDistance_impl(PyObject*, PyObject* o) { //double distance = askRealsenseDistance(); double distance = PyFloat_AsDouble(o)/100; connectRealsense(); return PyFloat_FromDouble(distance); } static PyMethodDef superfastcode2_methods[] = { // The first property is the name exposed to Python, fast_tanh, the second is the C++ // function name that contains the implementation. { "fast_tanh", (PyCFunction)tanh_impl, METH_O, nullptr }, { "askRealsenseDistance_py", (PyCFunction)askRealsenseDistance_impl, METH_O, nullptr }, // Terminate the array with an object containing nulls. { nullptr, nullptr, 0, nullptr } }; static PyModuleDef superfastcode2_module = { PyModuleDef_HEAD_INIT, "superfastcode2", // Module name to use with Python import statements "Provides some functions, but faster", // Module description 0, superfastcode2_methods // Structure that defines the methods of the module }; PyMODINIT_FUNC PyInit_superfastcode2() { return PyModule_Create(&superfastcode2_module); }

和RealsenseDitanceV3.py(来自RealsenseDistanceV3):

from itertools import islice from random import random from time import perf_counter from superfastcode2 import fast_tanh from superfastcode2 import askRealsenseDistance_py COUNT = 500000 # Change this value depending on the speed of your computer DATA = list(islice(iter(lambda: (random() - 0.5) * 3.0, None), COUNT)) e = 2.7182818284590452353602874713527 def sinh(x): return (1 - (e ** (-2 * x))) / (2 * (e ** -x)) def cosh(x): return (1 + (e ** (-2 * x))) / (2 * (e ** -x)) def tanh(x): tanh_x = sinh(x) / cosh(x) return tanh_x def test(fn, name): start = perf_counter() result = fn(DATA) duration = perf_counter() - start print('{} took {:.3f} seconds\n\n'.format(name, duration)) for d in result: assert -1 <= d <= 1, " incorrect values" #if __name__ == "__main__": #print('Running benchmarks with COUNT = {}'.format(COUNT)) #test(lambda d: [tanh(x) for x in d], '[tanh(x) for x in d] (Python implementation)') #test(lambda d: [fast_tanh(x) for x in d], '[fast_tanh(x) for x in d] (CPython C++ extension)') number = 8050 print('send: {} to cpp which divides it by 100'.format(number)) output = askRealsenseDistance_py(number) print('received from cpp: {}'.format(output))

为清楚起见,如果从module.cpp中删除了行rs2::pipeline p;或行connectRealsense();,一切正常。不知何故,我认为Python.h(CPython)库无法识别Realsense C ++函数。

希望您有一些建议,谢谢!

对于一个学校项目,我需要将Intel Realsense(3D摄像机)与Python连接起来,才能与RoboDK和OpenCV一起使用。我正在使用MVS 2019进行此操作。使用superfastcode2(C ++)和RealsenseDistanceV3(...

找到了解决方案!

您需要从输出库的Realsense库中复制.dll文件。发现后,这似乎很合逻辑,但首先我认为MVS将使.dll文件本身(来自整个代码)在Python中使用。事实证明并非如此,.dll文件仅来自Realsenselibrary。感谢大家的帮助!enter image description here
python c++ cpython
1个回答
0
投票

找到了解决方案!

© www.soinside.com 2019 - 2024. All rights reserved.