2

I have to compile those files in that order: a first and then b because of other requirements.

now the files are of this form: a.h

void caller_func(void);

a.c

#include "a.h"
#include "b.h"
void caller_func(void){
    called_func(void);
}

b.h

void called_func(void);

b.c

#include "b.h"
void called_func(void){
    //any action
}

and on compilation i get a.so: undefined reference to 'called_function'

is there anyway to go around this (besides changing compilation order)?

user1262882
  • 79
  • 1
  • 3
  • 1
    How do you invoke the compiler? Might it be the compiler not only tries to compile but also to link *a*? – JDS Jul 10 '14 at 21:32
  • Likely duplicate http://stackoverflow.com/questions/5559250/c-error-undefined-reference-to-function-but-it-is-defined – harmic Jul 10 '14 at 21:39
  • 1
    If you compile `a.c` to `a.o` and `b.c` to `b.o` then you must link `a.o` and `b.o` together. Or you can compile both together: `cc -o output a.c b.c` – rslemos Jul 10 '14 at 21:40

3 Answers3

2

The code, as written, should not have a problem. However, you must insure that you both compile a.c into a.o and b.c into b.o and link them.

Your compile line should look something like:

gcc a.c b.c -o a.out

Or:

gcc -c a.c b.c 
ld -o a.out a.o b.o 

In the first example, gcc will invoke ld behind the scenes to link the executable for you. In the second example, you're explicitly carrying out the compile and link steps.

stix
  • 1,140
  • 13
  • 36
1

I have to compile those files in that order: a first and then b because of other requirements.

No, you don't have this requirements. Both modules have nothing todo with each other.

What you have to do is maybe linking in the correct order if one of the modules is contained in a library. But I don't think that is the problem here.

Another hint: It is not important to include the declaration before the definition here if both are identical!

What you have to do is simple: compile & link in one stage like

 gcc a.c b.c -o prog

or compile in 2 steps ( order doesn't matter at all )

 gcc a.c -c
 gcc b.c -c

 gcc a.o b.o -o prog

All this is on the assumtion you take gcc on linux. But most other compilers will behave in the same way. Feel free to ask for special compilers or environments or OSs.

Klaus
  • 24,205
  • 7
  • 58
  • 113
1

"undefined reference to 'called_function'" means the linker can't find the machine code generated from compiling b.c; either you're not compiling that file, or the object code generated from compiling it isn't being included in the final link command.

Assuming gcc, you'd do something like

gcc -c a.c          # compiles a.c, generates object file a.o
gcc -c b.c          # compiles b.c, generates object file b.o
gcc -o prog a.o b.o # links the object files into an executable named prog

It sounds like you've written something like

gcc a.c

which compiles a.c, then attempts to link it into an executable (default name a.out or a.exe, depending on your platform).

John Bode
  • 119,563
  • 19
  • 122
  • 198