extern "Python" functions with external linkage

Issue #254 resolved
Michael McGee
created an issue

Functions marked as extern "Python" have static linkage in the generated code, but it is often desirable to call these functions from C code in other objects.

Simple workaround is to write a wrapper which exports the function with external linkage, but it would be nice to not have to do this.

Comments (5)

  1. Armin Rigo

    Do you have an idea about how to specify this? There are various project- and compiler-specific ways to define the linkage of functions. Generating int f(int) { xxx } without static is the most obvious one, but there are others. For example, with gcc in some cases you want __attribute__((visibility("hidden"))) int f(int) { xxx }. Or, with MSVC you'd like to control the calling convention: int WINAPI f(int) { xxx }.

  2. Michael McGee reporter

    My thought would have been external linkage the default (as in C), and a static specific is optional, like:

    static extern "Python" f();

    Maybe for back compat you'd want static to be the default, so to make it extern

    extern extern "Python" f();

    Hadn't thought about it too much, another approach might be to copy certain symbols into the source declaration directly, like

    SYM_A extern "Python" SYM_B f();

    Then the user can #define SYM_A static and #define SYM_B __stdcall etc as needed.

  3. Armin Rigo

    A minimal change would be extern "Python+C" int f(int);, which means it's both an extern "Python" and extern in the C sense of the word.

    It might be enough for now. I think that if you need to fine-tune some gcc attributes you can also write manually in the set_source() (or in some .h file included from there) a header for the function. The real problem you're trying to fix is only that a static function cannot follow a non-static header declaration.

  4. Log in to comment