0

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?
}
Parth
  • 123
  • 6
  • 1
    I feel like this was unfairly closed. The other question does not explain what pointers are and completely misses the first part of this question. Please re-open. – Tino A. Jun 24 '16 at 12:09
  • @TinoA. I think I have well explained enough what `extern "C" {}` does, when it's enclosing c++ code in my answer there. – πάντα ῥεῖ Jun 24 '16 at 12:14
  • @πάνταῥεῖ Correct. but the user apparently does not know about pointers in C++ or does not understand them and I feel like this was a large part of this question. Also this is not a pure c/c++ question but also a python one. – Tino A. Jun 24 '16 at 12:15
  • @TinoA. No, if there are confusions about pointers, this should be addressed in a different question. – πάντα ῥεῖ Jun 24 '16 at 12:16
  • 2
    I think the previous answer will help you. In your example, you need to move the brace `{` as follows: `extern "C" { int fib(int a) { /* body of function */ } }` – John D Jun 24 '16 at 12:23
  • @πάνταῥεῖ I have little experience with C++ and as TinoA. said, I do not understand how pointers are being used here. – Parth Jun 24 '16 at 15:25
  • @πάνταῥεῖ I have edited the question, please check if it is specific enough now. – Parth Jun 24 '16 at 15:28
  • _@uncertainty_principle_ The answer was given in @John's comment already. What do pointers have to do with that actually? – πάντα ῥεῖ Jun 24 '16 at 15:29
  • "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 is this being answered in his comment? – Parth Jun 24 '16 at 15:31
  • 1
    I thought the first part was an example. That's pretty advanced stuff. `Foo_new` returns a pointer to a new object of type `Foo`. `Foo_bar` receives a pointer to a `Foo` object and calls its `bar` method to print "Hello". The `foo->bar()` means that `foo` is a pointer and I want the object it points to, then call `bar` on that object. `extern "C"` is really just the beginning. It just removes the C++ name mangling. You need to understand that first, then build a DLL and export those two functions. – John D Jun 24 '16 at 18:26
  • 1
    If you need help with building the DLL and calling it from Python, I suggest you post a new question with your DLL code, your Python code and give it a title like "What is going wrong with this C++ DLL call from Python". If you're not comfortable with pointers, your `fib` function is a better place to start. In Python it would be `f = lib.fib(5)` where `lib = cdll.LoadLibrary('MyDLL.dll')` using full pathname. – John D Jun 24 '16 at 18:55
  • @JohnD That is very helpful, thanks a lot! Yeah I've already built the DLL by writing the same code in C instead of C++ so I'm not too confused about that. I'll try again with C++ and see if it works. – Parth Jun 24 '16 at 22:32

0 Answers0