1

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?

4
  • Not really a C++ question -- the above code and usage is C -- changed the tags to be C Commented May 13, 2014 at 4:40
  • @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). Commented May 13, 2014 at 4:44
  • @paddy -- good points, agree on both... Commented May 13, 2014 at 4:49
  • oh, it is a mistake and fixed :-) Commented May 13, 2014 at 6:44

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.