Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do C callbacks require extern "C"? #78

Open
ivan-pi opened this issue Jul 4, 2023 · 3 comments
Open

Do C callbacks require extern "C"? #78

ivan-pi opened this issue Jul 4, 2023 · 3 comments

Comments

@ivan-pi
Copy link
Member

ivan-pi commented Jul 4, 2023

Assuming the C interface of MINPACK is used in C++ program, should the callback types also have C linkage specified?

extern "C" {                  // <-- do we need this?
typedef void (*minpack_func)(
    int /* n */,
    const double* /* x */,
    double* /* fvec */,
    int* /* iflag */,
    void* /* udata */);
}                             // <-- do we need this?

The last section of this Oracle document, Pointers to Functions as Function Parameters, appears to say that unless the linkage is specified, the following declaration

MINPACK_EXTERN void MINPACK_CALL
minpack_hybrd(
    minpack_func /* fcn */,
    int /* n */,
    double* /* x */,
    double* /* fvec */,
    ...);

assumes the fcn argument will have C++ linkage and not C, like expected by the Fortran interfaces (definitions).

Typically, C and C++ compilers of the same vendor will be binary compatible when it comes to function linkage, but in principle they don't have to be.

@certik
Copy link
Member

certik commented Jul 4, 2023

Yes, you have to put in extern "C" for C++. You typically put it under defines to only be used in C++ codes, not C.

@ivan-pi
Copy link
Member Author

ivan-pi commented Jul 4, 2023

We have that in MINPACK_EXTERN macro for the procedures, but not for the callback typedefs. According to the Oracle resources, the typedefs should have it too.

@certik
Copy link
Member

certik commented Jul 4, 2023

Ah I see. Looks good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants