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.

My operating system is windows 10. Here is the code in c++

#include <iostream>

struct Math
{

    void print()
    {
        std::cout << "hehe" << std::endl;
    }

};

extern"C"
{
    Math* newMath()
    {
        return new Math();
    }

    void math_print(Math* math)
    {
        math->print();
    }
}

Code in python

from ctypes import cdll
lib = cdll.LoadLibrary('./math.so')

class Math:
    def __init__(self):
        self.object = lib.newMath()

    def print(self):
        lib.print = (self.object)

math = Math()
math.print()

Compilation of .o and .so files

g++ -c -fPIC math.cpp -o math.o 
g++ -shared -Wl,-soname,math.so -o math.so  math.o

Error message

self._handle = _dlopen(self._name, mode)
OSError: [WinError 1114] A dynamic link library (DLL) initialization routine failed

Both my python interpreter and g++ compiler are 64 bits.

After n.'pronouns'm's comment about linkers, I decided to remove the #include<iostream> from c++ file to see what happens, and it sort of worked. I changed the c++ code to

//#include <iostream>

struct Math
{

    int print()
    {
        return 5;
    }

};

extern"C"
{
    Math* newMath()
    {
        return new Math();
    }

    void math_print(Math* math)
    {
        math->print();
    } 

and python code to

from ctypes import cdll
lib = cdll.LoadLibrary('./math.so')

class Math:
    def __init__(self):
        self.object = lib.newMath()

    def print(self):
        lib.math_print = (self.object)

math = Math()
print(math.print())

I would expect it to print 5, but it prints none and no error this time. I will also definitely need libraries in the future to access the math functions.

  • Linux style compilation and library naming convention followed by windows style DLL error. What system are you working on? Also, once you got it working, I think that python will interpret the pointer as integer. – pptaszni Nov 07 '20 at 19:44
  • @pptaszni I am working on windows 10 –  Nov 07 '20 at 19:46
  • 1
    Uhh, fastest solution would be to use Linux :) (which you can also quickly install install inside windows). I don't remember exact steps to do it on windows, but you need to build DLL first and then load it with python cdll. Also your `print` function is wrong. Should be `lib.math_print(self.object)` – pptaszni Nov 07 '20 at 19:52
  • Cannot reproduce this. Do you run python in the same shell you build your library? – n. m. could be an AI Nov 08 '20 at 09:46
  • @n.'pronouns'm. I ran python from IDLE by pressing f5 –  Nov 08 '20 at 09:48
  • This could be your problem, IDLE probably doesn't know where your C++ runtime libraries are. Try running python from the command line in the same shell you run g++ from. – n. m. could be an AI Nov 08 '20 at 09:51
  • @n.'pronouns'm. Just tried doing that but still getting the same error. Just to be sure, I recompiled the .o and .so files from cmd and than ran python from the same cmd window. Is that what you meant? –  Nov 08 '20 at 09:57
  • Yes, this exact procedure works for me. Can you compile and run a hello world C++ program from the same cmd? – n. m. could be an AI Nov 08 '20 at 10:02
  • @n.'pronouns'm. It might be my g++. I did try reinstalling it but its still not working. My g++ version is `g++ (tdm64-1) 9.2.0`. Which one are you using? Can you send a download link please. Maybe it will work with your version –  Nov 08 '20 at 10:07
  • 1
    I use msys2, gcc is a package in it. Have you tried to compile and run a hello world program? – n. m. could be an AI Nov 08 '20 at 10:11
  • Yes i have tried doing that and it works. Also, I edited the question. –  Nov 08 '20 at 10:22
  • Please, for the future, before even asking a question, create a [mcve]. In particular, does it work for a single function returning a simple integer? As it stands, you now have two different questions concerning two different versions of code mixed in one, which isn't good. – Ulrich Eckhardt Nov 08 '20 at 13:03
  • @UlrichEckhardt It isn't really 2 different questions. I just commented out the line that was causing problems in the second example but I am still looking for a solution. But yea sure, ill be careful next time –  Nov 08 '20 at 13:34
  • It is likely that python cannot find libstdc++. Try adding the directory where that library is located to your PATH environment. You can also try and look at the library with a tool such as [dependency walker](http://www.dependencywalker.com/) and see what is missing. – n. m. could be an AI Nov 08 '20 at 15:17

0 Answers0