Python3.4 ctypes包装用于基于C的mingw-w64编译dll

Python3.4 ctypes包装用于基于C的mingw-w64编译dll,python,c,dll,ctypes,mingw-w64,Python,C,Dll,Ctypes,Mingw W64,我在将mingw-w64编译的dll包装到带有ctypes的Py3.4中时遇到问题 最小(非)工作示例: /* 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;

我在将mingw-w64编译的dll包装到带有ctypes的Py3.4中时遇到问题

最小(非)工作示例:

/* 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;
}
我在python代码的最后一行得到一个错误(AttributeError:function'gcd'not found):

# 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
<>我有一个C++中的最小例子,它的工作过程与上面类似。我做错了什么

我发现这个问题与此类似: 但我无法将其注册为COM对象(未找到入口点DllRegisterServer):

你在用g++编译,所以名字可能是被损坏的,C++风格。使用gcc编译或将函数声明为
extern“C”
。谢谢@eryksun!我使用了gcc,解决了这个问题!你在用G++编译,所以名字可能是被损坏的,C++风格。使用gcc编译或将函数声明为
extern“C”
。谢谢@eryksun!我使用了gcc,解决了这个问题!你在用G++编译,所以名字可能是被损坏的,C++风格。使用gcc编译或将函数声明为
extern“C”
。谢谢@eryksun!我使用了gcc,解决了这个问题!
g++ -c sample.c
g++ -shared -o sample.dll sample.o -Wl,--out-implib,libsample.a
# 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