0

Can Someone Explain this to me?

The file a is as follows:

file a.c
#include <stdio.h>
#include <stdlib.h>

int fun1();
int main()
{
     fun1();
     return 0;
}

File b is written as:

file b.c
static int fun1();
int  fun1(){
     printf("fron fun1");
     return 0;
}

When I try to compile my code, the compiler gives an error: undefined reference to fun1. What does it mean if I do declare fun1 static in file b.c?

Duggs
  • 169
  • 1
  • 12
  • 3
    `static` means the scope of the function is limited to the module (file) you declared it in. So you need to remove the `static` declaration in file `b.c`. With the `static` declaration in `b.c`, `fun1` is not accessible to `a.c`. – lurker Aug 02 '13 at 17:16
  • 1
    Are you sure this is a compiler error and not a linker error? – Dabbler Aug 02 '13 at 17:18
  • Please post the command line that you use to compile this. You are probably looking at a linker error as @Dabbler said. – Sergey L. Aug 02 '13 at 17:22
  • yeah sorry i posted carelessly it was a linker error i edited my question – Duggs Aug 02 '13 at 17:23
  • Then make sure to link both object files together, and remove `static` from the declaration of `fun1`. – Dabbler Aug 02 '13 at 17:25
  • Drop `static`. `cc -c a.c && cc -c b.c && cc a.o b.o` should compile then. – Sergey L. Aug 02 '13 at 17:25

5 Answers5

3

if you declare fun1 as static in b.c, it will not be visible externally, so you can only use it from within the compilation unit b.c. Drop the static.

Note: To be able to compile your code, you also need to add #include <stdio.h> at the top of b.c.

flyx
  • 35,506
  • 7
  • 89
  • 126
0

Use extern in a.c as extern int fun1();

Note : fun1() has external linkage by default.

and remove static int fun1(); from b.c

Compile Using :

gcc -o test a.c b.c

P0W
  • 46,614
  • 9
  • 72
  • 119
  • `extern` is optional and has little impact on the compilation. – flyx Aug 02 '13 at 17:22
  • @flyx ouhh yes, just for readability, I shouldn't have though, thanks – P0W Aug 02 '13 at 17:31
  • Readability is a good thing and worth mentioning. When it comes to readability, I'd prefer header files to external declarations in implementation files. – flyx Aug 02 '13 at 17:36
0

well, you are asking for this behaviour in the file b.c.

when you declare something static like a function, that function is visible only in the same translation unit where it's been declared, in other words you have a function in b.c that only exist and therefore can be used in b.c.

so your linker is considering the function inside b.c not as an option and this leaves you with an undefined reference because no other implementations in this code are able to solve this symbol.

user2485710
  • 9,451
  • 13
  • 58
  • 102
0

Reason for compiler error: In file "a.c" there is no definition of the function fun1().

Solution 1:

declare fun1() as extern in "a.c" : extern int fun1();

Solution 2:

Add #include "b.c" in "a.c"
Nalaka526
  • 11,278
  • 21
  • 82
  • 116
0

The static function which you have defined is visible only in that file i.e. it is local to that translation unit.

That is why the compiler(linker) is unable to find the definition of that function. Remove the static keyword.

For Compilation only the function prototype is enough, but while linking you have to make sure that the function which you claimed to exist during the compile time does exist in some other files. But by declaring it as static you're limiting the definition of the function to only that file and not making it visible to the file which needs(calls) it.

Uchia Itachi
  • 5,287
  • 2
  • 23
  • 26