python - 在 python 中使用 c++ 函数时出错:OSError: [WinError 193] %1 is not an valid Win32 application
问题描述
我正在尝试用 c++ 为我用 python 制作的游戏编写一个数学库。我尝试按照 Florian Bosch 在这个问题中回答的步骤进行操作。
这是c ++中的代码
#include <iostream>
class math
{
public:
void print()
{
std::cout << "hello" << std::endl;
}
};
extern "C"
{
__declspec(dllexport) math* mathNew()
{
return new math();
}
__declspec(dllexport) void assignMath(math* mathObject)
{
mathObject->print();
}
}
这是python中的代码。
from ctypes import cdll
lib = cdll.LoadLibrary('./math.so')
class Math:
def __init__(self):
self.object = lib.mathNew()
def print(self):
lib.print = (self.object)
math = Math()
math.print()
这是我制作 .o 和 .so 文件的方法。
g++ -c -fPIC math.cpp -o math.o
g++ -shared -Wl,-soname,math.so -o math.so math.o
这是我收到的完整错误消息。
Traceback (most recent call last):
File "C:\Users\me\OneDrive\Documents\A level python codes\project-actual proper final\main.py", line 2, in <module>
lib = cdll.LoadLibrary('./math.so')
File "C:\Users\me\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.py", line 447, in LoadLibrary
return self._dlltype(name)
File "C:\Users\me\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.py", line 369, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
File "C:\Users\me\AppData\Local\Programs\Python\Python38\lib\ctypes\__init__.py", line 369, in __init__
self._handle = _dlopen(self._name, mode)
OSError: [WinError 193] %1 is not a valid Win32 application
解决方案
推荐阅读
- c# - asp.net mvc 路由捕获 /{sometoken}
- python - 在 C++ 中将图像数据从 python 传递到 cv::mat
- javascript - VueJS拼接从数组中删除所有项目
- windows - 并行执行 shell 命令但限制作业(没有 Cygwin 的 Windows)
- c - 在 C 中创建字符串值并将其添加到二维数组
- python - pymongo.errors.ServerSelectionTimeoutError
- sql - 更新期间出错
- python - 用python 3编写的单词搜索拼图生成器陷入无限循环
- javascript - 使用 AJAX / JSON 数据将图像 ID 传递给模态
- c# - REGDB_E_CLASSNOTREG 在应用程序池回收后使用 MsgPack Serializer 时