jaeyong
jaeyong

Reputation: 9363

Linking static function from another file in c

I have two source files: A.c and B.c

A.c has a function, call_me:

static int call_me();

void call_me_register()
{
    register_call_me(call_me);
}

As you can see, call_me function is used as variable so it has a symbol of call_me in A.o

Now, I want to call this call_me function in B.c file.

static int call_me();

void call_call_me()
{
    call_me();
}

If I try to link B.o, I've got an link error, that no such reference of call_me.

Here, the constraint is the following: I can not modify A.c for some reason. Is there a any to call 'call_me' in A.c in B.c?

Upvotes: 1

Views: 1513

Answers (2)

Arif
Arif

Reputation: 31

Visibility of Static functions in C is restricted to only the file in which they are declared.so if u declare one in A.c and call that function from another file b.c you will get a link error that the function is unreferenced..Hope this will solve your problem..

Upvotes: 3

Soren
Soren

Reputation: 14688

The register function in A.cpp will presumably store a pointer to the function somewhere (hence that is the registration) and you can use that pointer to call the function. -- so looks for a function to fetch the registered call_me function.

You cannot reference the function directly -- as it is static -- and being declared static as in you sample code means that the function is hidden for the outside world.

Upvotes: 0

Related Questions