I've been following this tutorial to try and wrap a C++ function using extern "C". I want to access this function using ctypes in Python. In the tutorial, the author creates a class "Foo" and then wraps it with C using extern. I'm having a hard time implementing it correctly because I can't understand what the code in extern "C" here does:
extern "C" {
Foo* Foo_new(){ return new Foo(); }
void Foo_bar(Foo* foo){ foo->bar(); }
}
Can someone explain what the author is doing in these two lines? Specifically, I'd like to know how pointers are being used here, what is the point of creating Foo_new() and what does the syntax in foo->bar() mean.
How would you implement this on a simple fibonacci function like the one that follows?
int fib(int a){
if (a<=0)
return -1;
else if (a==1)
return 0;
else if ((a==2)||(a==3))
return 1;
else
return fib(a-2) + fib(a-1);
};
extern "C" int fib(int a){
// what do I do here?
}