У меня проблемы с переносом скомпилированной mingw-w64 dll в py 3.4 с ctypes.
Минимальный (не) рабочий пример:
/* 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;
}
Заголовочный файл
/* sample.h */
#include <math.h>
extern int gcd(int, int);
Я компилирую его с помощью:
g++ -c sample.c
g++ -shared -o sample.dll sample.o -Wl,--out-implib,libsample.a
И я получаю сообщение об ошибке (AttributeError: функция 'gcd' не найдена) в последней строке этого кода Python:
# 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
У меня есть минимальный пример, написанный на С++, и он работает с той же процедурой, что и выше. Что я делаю не так?
Я обнаружил, что эта проблема похожа: VB dll не работает в python с ctypes (функция * не найдена) Но мне не удалось зарегистрировать его как COM-объект (точка входа DllRegisterServer не найдена): Как зарегистрировать COM-объект для всех пользователей
extern "C"
. - person Eryk Sun   schedule 12.06.2015