ich versuche momentan eine c dll über ctype in meinem Python Skript einzubinden.
Bisher hatte ich damit leider nur mäßigen Erfolg.
Daher wende ich mich jetzt an Euch, in der Hoffnung, dass mir jemand helfen kann.
Mein Code sieht wie folgt aus:
foo.h
Code: Alles auswählen
#ifndef GUARD_FOO
#define GUARD_FOO
#define NUMB 2
#include<stdint.h>
typedef struct Type
{
int re, im;
} Type;
uint8_t func(Type *fout[NUMB], int start, int end);
#endif // GUARD_FOO
Code: Alles auswählen
#include<stdio.h>
#include<stdint.h>
#include"foo.h"
uint8_t func(Type *fout[NUMB], int start, int end)
{
for(int i=0;i<NUMB;i++)
{
Type* ptr = fout[i];
for(int o=start;o<end;o++)
{
printf("%d %d re: %d im: %d\n", i, o, (ptr+o)->re, (ptr+o)->im);
(ptr+o)->re = o;
(ptr+o)->im = o;
printf("%d %d re: %d im: %d\n", i, o, (ptr+o)->re, (ptr+o)->im);
}
}
return 0;
}
Code: Alles auswählen
OBJS = foo.o
SOURCE = foo.c
HEADER = foo.h
OUT = foo.dll
CC = gcc
FLAGS = -g
LFLAGS = -fPIC -shared -Wall
library: $(OBJS)
$(CC) $(LFLAGS) -o $(OUT) $(OBJS)
object: foo.c
$(CC) $(FLAGS) $(SOURCE)
clean:
rm -f $(OBJS) $(OUT)
Code: Alles auswählen
from ctypes import *
import numpy as np
class Type(Structure):
_fields_=[("re", c_int),
("im", c_int)]
def __repr__(self):
return 're: {0} im: {1}'.format(self.re, self.im)
lib=cdll.LoadLibrary("foo.dll")
print(lib)
func=lib.func
print(func)
func.argtypes=[POINTER(Type)*2, c_int, c_int]
func.restype=c_uint8
arr_0=np.zeros(shape=(100,), dtype=Type)
arr_1=np.ones(shape=(100,), dtype=Type)
##
#
# ???
#
##
func(???. 2, 10)
Ich habe eine Funktion func welcher ich ein Pointer Array des Datentyp "Type" übergeben muss.
Dieses Pointer Array beinhält die Anfangsadressen meiner beiden numpy Arrays „arr_0“ und „arr_1“.
Kann mir jemand erklären wie ich das bewerkstellige?
Gruß,
Robin