I am trying to write a math library in c++ for a game I made in python. I tried following the steps from answer by Florian Bosch in this question.
Here is the code in 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();
}
}
Here is the code in 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()
Here is how I made the .o and .so file.
g++ -c -fPIC math.cpp -o math.o
g++ -shared -Wl,-soname,math.so -o math.so math.o
Here is the full error message I am getting.
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