Python:ctypes.cdll无法加载c ++编译的共享.dll文件

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

.dll

我已经检查了这条路,甚至使用了全路,从未工作过,一整天都在尝试,一次又一次地以不同的方式和方法来编译
Traceback (most recent call last): File "e:\My Apps\Python\Telegram_Bots\Calculator\main.py", line 6, in <module> lib = CDLL("E:\My Apps\Python\Telegram_Bots\Calculator\lib.dll") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "c:\Users\KHATAM_PC\AppData\Local\Programs\Python\Python311\Lib\ctypes\__init__.py", line 376, in __init__ self._handle = _dlopen(self._name, mode) ^^^^^^^^^^^^^^^^^^^^^^^^^ FileNotFoundError: Could not find module 'E:\My Apps\Python\Telegram_Bots\Calculator\lib.dll' (or one of its dependencies). Try using the full path with constructor syntax.
,但没有任何东西,但它们都在C ++
中工作。

.dll
出口命令:

.dll

g++ -DMATH_DLL_EXPORTS -shared -std=c++17 -o lib.dll lib.cpp

lib.hpp
// #pragma once

#ifndef MATH_DLL_API
#define MATH_DLL_API

#ifdef _WIN32
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif

#include <iostream>
#include <vector>
extern "C" {
    DLL_EXPORT int precedence(char o);
    DLL_EXPORT float calc(float a, float b, char o);
    DLL_EXPORT void calculate(std::vector<float>& values, std::vector<char>& ops);
    DLL_EXPORT float evaluate(std::string e);
}
#endif // MATH_DLL_API

lib.cpp

最后,python脚本:
#include "lib.hpp"
#include <iostream>
#include <vector>
extern "C" {
    DLL_EXPORT int precedence(char o) { ... }
    DLL_EXPORT float calc(float a, float b, char o) { ... }
    DLL_EXPORT void calculate(std::vector<float>& values, std::vector<char>& ops) { ... }
    DLL_EXPORT float evaluate(std::string e) { ... }
}

有人可以帮助吗?

here是一个可以加载它的DLL和Python脚本的简单示例。这是使用Microsoft的C ++编译器而不是GNU版本构建的。

from ctypes import c_char, c_int, c_float, c_char_p, c_void_p, CDLL, cdll, POINTER lib = CDLL("E:\My Apps\Python\Telegram_Bots\Calculator\lib.dll") lib.evaluate.argtypes = [c_char_p] lib.evaluate.restype = c_float lib.calculate.argtypes = [POINTER(c_float), POINTER(c_char)] lib.calculate.restype = c_void_p lib.calc.argtypes = [c_float, c_float, c_char] lib.calc.restype = c_float lib.precedence.argtypes = [c_char] lib.precedence.restype = c_int

//
// zzdll.h header 
//
#pragma once

#ifdef ZZDLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT
#endif

extern "C" {
    DLL_EXPORT int precedence(char o);
    DLL_EXPORT float calculate(float a, float b);
}

// // zzdll.cpp implementation file // #include <iostream> #define ZZDLL #include "zzdll.h" int precedence(char o) { std::cout << "zzdll::precedence('" << o << "')\n"; return 137; } float calculate(float a, float b) { std::cout << "zzdll::calc(" << a << ", " << b << ")\n"; return float(a * b); }

python c dll c++17 ctypes
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.