Skeen
Skeen

Reputation: 4722

C linking C++ static functions

my question is somewhat simple;

Is it possible to do C linking (extern "C") on C++ static class functions? - Without using wrappers.

Upvotes: 2

Views: 238

Answers (3)

Pete Becker
Pete Becker

Reputation: 76235

extern "C" can't be applied to a static member function. But since the goal is to call the function from assembler, just use the mangled name in the assembler code. There's nothing magic here; all you need is the name.

Upvotes: 1

As I said in the comments, you can't. But you can achieve the same net effect.

Here's what I was talking about:

class A;
extern "C" void foo(A*);


class A
{
    int i;
    friend void foo(A*);
};

extern "C" void foo (A* a)
{
    a->i = 10;
}

int main()
{
    A a;
    foo(&a);
    return 0;
}

Compiles fine on gcc 4.7.2 here

Upvotes: 2

john
john

Reputation: 8027

No you can't but simply write a wrapper function.

class X
{
public:
    static void f();
};

extern "C" void call_X_f()
{
    X::f();
}

Upvotes: 5

Related Questions