Python 3.4 ctypes wrap for a mingw-w64 compiled C-based dll

524 views Asked by At

I have problems wraping a mingw-w64 compiled dll into py 3.4 with ctypes.

Minimal (not) working example:

/* sample.c */
#include <math.h>

/* Compute the greatest common divisor */
int gcd(int x, int y) {
    int g = y;
    while (x > 0) {
        g = x;
        x = y % x;
        y = g;
    }
    return g;
}

Header file

/* sample.h */
#include <math.h>

extern int gcd(int, int);

I am compiling it with:

g++ -c sample.c
g++ -shared -o sample.dll sample.o -Wl,--out-implib,libsample.a

And I get an error (AttributeError: function 'gcd' not found) at the last line of this python code:

# sample.py
import ctypes
import os

# Try to locate the file in the same directory as this file
_file = 'sample'
_path = os.path.join(*(os.path.split(__file__)[:-1] + (_file,)))
_mod = ctypes.cdll.LoadLibrary(_path)

gcd = _mod.gcd

I have an minimal example written in c++ and it works with a similar procedure than above. What am I doing wrong?

I found this problem to be similar: VB dll doesnt work in python with ctypes (function * not found) But I was not able to register it as a COM object (the entry-point DllRegisterServer was not found): How to Register a COM Object for All Users

0

There are 0 answers