2

This is not a problem to fix or something.Just want to know the behaviour

#include <stdio.h>
extern int var;
int main()
{
    var = 10;
    printf("%d ", var);
    return 0;
}

Here we can see that var was defined as extern and inside the main() function var was given definition. But when the var was outputted it gave an error

undefined reference to var.

Then where does the value 10 go? Will it go to extern var or it is stored in a garbage memory location?

So what exactly happens during compilation at line var =10 and the next line.

Compile Log:

Compilation error   time: 0 memory: 2156 signal:0
/home/PpnviQ/ccRtZapf.o: In function `main':
prog.c:(.text.startup+0x13): undefined reference to `var'
collect2: error: ld returned 1 exit status
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 2
    `var` is not defined in the code shown. Just declared. – too honest for this site Jul 07 '16 at 15:20
  • @Olaf what exactly ```var =10``` means,it does'nt mean defining –  Jul 07 '16 at 15:21
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. – too honest for this site Jul 07 '16 at 15:21
  • I'm very confident my comment implied that! Was not me writing "And inside the main function a var was given definition" But maybe you mena something else. Please clarify and see [ask]. – too honest for this site Jul 07 '16 at 15:21
  • 1
    @GoogleBot It means *assignment*. You tell the compiler: There is a a variable `var` out there, let's assign it with `10`. Compiler says: "OK! Let's do it". And then comes the linker and says: "Hey guys, you told me that there is a variable out there, but I don't know about it". – Eugene Sh. Jul 07 '16 at 15:23
  • @EugeneSh. can you explain interms of memory location(Taking a dummy address) –  Jul 07 '16 at 15:24
  • Please read this: http://stackoverflow.com/a/1433387/10077 – Fred Larson Jul 07 '16 at 15:24
  • @GoogleBot What memory locations? That's exactly the problem here, that there is no memory locations. – Eugene Sh. Jul 07 '16 at 15:25
  • You should have `int var;` in another file. Extern means you define the variable in other place and you need to use that in this file. – G. Emadi Jul 07 '16 at 15:25
  • @G.Emadi: `exterrn int var` is a **declaration**, not a definition! Please be precise when using standard terms. – too honest for this site Jul 07 '16 at 15:32
  • 1
    the behaviour is that without a proper declaration there's not a memory allocation to hold the variable `var` ... thus the complaint of the linker... – weirdgyn Jul 07 '16 at 15:32
  • 1
    @weirdgyn: And another one: There is a declaration! The problem seems to be the missing **definition**! But without more information, it is hard to tell. – too honest for this site Jul 07 '16 at 15:33
  • sorry .. it was definition... got lost in the middle ;-) – weirdgyn Jul 07 '16 at 15:35
  • @Olaf .. no need for further info.. I saw this type of error dozen of times. – weirdgyn Jul 07 '16 at 15:36

1 Answers1

9

In your code,

 extern int var;

is a declaration, not a definition. So, in the complete translation unit, var is never defined. So your linker (to be specific) complains when you try to use it, (assign value to var).

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • 1
    the compiler is fine because declaring `extern` references just warn it to wait for a definition somewhere .. usually `extern` keyword is used to refer to variables declared in other units so it's normal that the compiler accept it.. probably you have a warning from the linker that found the reference left dangling.... – weirdgyn Jul 07 '16 at 15:24
  • @LPs thnx.. just fixed this! :-) – weirdgyn Jul 07 '16 at 15:27
  • @SouravGhosh what if i defined ```int var =25``` in another file and include it here. –  Jul 07 '16 at 15:32
  • 2
    @GoogleBot if you have the variable defined in some other translation unit which you're linking together, then your linker will be able to find the variable, and there'll be no issues. – Sourav Ghosh Jul 07 '16 at 15:34