1
votes

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?

2
Not really a C++ question -- the above code and usage is C -- changed the tags to be CSoren
@Soren It kinda is C++, because the function name register_call_me is overloaded (but the more I look at it, the more it seems like a mistake).paddy
@paddy -- good points, agree on both...Soren
oh, it is a mistake and fixed :-)jaeyong

2 Answers

3
votes

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..

0
votes

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.