0

I have my file where I have declared functions function1 and function2

include.h in /dir1/dir2/dir3/dir4/

I have defined all the functions and variables in

file include.c at path /dir1/dir2/dir3/dir5/

and I am using these functions at path

/dir1/drr2/dir3/dir6/

my pseudo code is ...

#include "include.h"
extern int function1();
extern void function(); 
...

function release()
{
  int i = function1();
} 

after compiling following error is thrown :

undefined reference to 'function1'
relocation truncated to fit: R_MIPS_26 against 'function1'  

I know that when we use extern we have to compile both the files but I am currently working with a OS161 and cannot compile files individually. i have to complile all the OS files together and then install them into kernel and finally boot it.

Where is the mistake?

alk
  • 69,737
  • 10
  • 105
  • 255
user1079065
  • 2,085
  • 9
  • 30
  • 53

1 Answers1

0

If you declare a symbol as extern, that means that it will not be defined in this translation unit. Instead, the linker will search for the symbol at link-time, searching all object files and libraries, and print an error message such as this if it can't find it.

You need to make sure that during linking, each object file that uses the extern function will be linked with the object generated from include.c.

Functions, other than global variables, are by default declared extern, so that can't be the issue here.

To compile multiple translation units together, just pass them all to the compiler at once.

mic_e
  • 5,594
  • 4
  • 34
  • 49
  • I have declared the functions as extern in include.h and anyways I am compiling all the OS files together – user1079065 Apr 01 '14 at 16:52
  • In that case, there shouldn't be an issue. What's your linker invocation? Usually you would do `gcc a.c; gcc b.c; gcc a.o b.o`, where the last would be the linker invocation. – mic_e Apr 01 '14 at 16:54
  • no its like fire following commands for entire source code of os161 make depend -- successful make -- giving error for undefined error – user1079065 Apr 01 '14 at 16:57
  • I'm sorry, but in that case I can't help you. You should try isolating the issue into two or three single files, where you can invoke the compiler manually. Have you tried whether removing `extern` solves the issue? – mic_e Apr 01 '14 at 17:03