0

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
  • 1
    Perhaps you should check that library and python interpreter are either both 32-bit or both 64-bit. Also libraries on windows typically have .dll file extension. – user7860670 Nov 06 '20 at 20:30
  • @user7860670 I am not sure what you mean by library. –  Nov 06 '20 at 20:34
  • the library you are trying to load. – user7860670 Nov 06 '20 at 20:39
  • @user7860670 I did python-VV in command line and its 64 bit. I am not sure how to check what bit the library is. I am using visual studio. It did say x86 in the dropdown box at the top in visual studio. I changed it to x64 just now, re-compiled the `.o ` and `.so` but it still gave the same error. –  Nov 06 '20 at 20:43
  • you're not using visual studio your command line is g++. it's clearly a 32/64 bit issue. – Jean-François Fabre Nov 06 '20 at 20:45
  • @Jean-FrançoisFabre Yea, visual studio is just the text editor –  Nov 06 '20 at 20:46
  • @Jean-FrançoisFabre So how would i change the library to 64 bit? –  Nov 06 '20 at 20:46
  • install a 64-bit compiler. Your g++ is 32 bit. – Jean-François Fabre Nov 06 '20 at 20:46
  • You could try `dumpbin /headers math.so` and see if it says `machine (x64)`. I'm not sure if Windows cares about the extensions of dlls, but changing to .dll makes sense too. – tdelaney Nov 06 '20 at 21:19
  • For what its worth, a bit of cleanup (removing `__declspec` and some other things) and it works on linux. – tdelaney Nov 06 '20 at 21:20

0 Answers0