-8

Please see the below example code for the use of "extern". When I use the extern keyword in my code, I get a compilation error. Please suggest a solution for the problem.

#include<iostream>

  extern int x;
  extern int y;
  extern int z;

int main(){
    
    x = 10;
    y = 15;
    
    z = (x>y ? x: y);
    
    std::cout<<z;
     
    return 0;
}

Error message:

example8.cpp:(.rdata$.refptr.z[.refptr.z]+0x0): undefined reference to `z';
example8.cpp:(.rdata$.refptr.y[.refptr.y]+0x0): undefined reference to `y';
example8.cpp:(.rdata$.refptr.x[.refptr.x]+0x0): undefined reference to `x';
F:\DEVC_workspace\collect2.exe  [Error] ld returned 1 exit status
Boann
  • 48,794
  • 16
  • 117
  • 146
saien
  • 5
  • 4
  • 9
    tip: change your point of view. It is more likely that your expectations are wrong rather than extern is "not working". What do you think is the effect of `extern` in this code? Also it is not clear for what "problem" you are searching a solution. You can simply remove `extern` and the "problem" is gone – 463035818_is_not_an_ai Aug 19 '20 at 14:55
  • `extern int x;` means "declaring that `x` has this type, but is not defined here." But you never define `x` anywhere. – Eljay Aug 19 '20 at 14:59
  • And the error messages are not compilation errors. They are linker errors. – Peter Aug 19 '20 at 15:00
  • 3
    The patient says, "Doctor, it hurts when I do this". The doctor says, "Then don't do that!" – n. m. could be an AI Aug 19 '20 at 15:08
  • 6
    your question is similar to "When I declare a variable as const, I cannot modify it. Why is `const` broken?". You have to explain your line of reasoning so others can point out your misunderstanding – 463035818_is_not_an_ai Aug 19 '20 at 15:10
  • @idclev463035818 alternatively, others can simply explain what `const` does and let OP do the reasoning :-) – Jeffrey Aug 19 '20 at 15:11
  • @Jeffrey true. Sometimes I am too much trying to understand the deeper meaning of the question while others already wrote a good answer by simply taking it literally. Imho both approaches have their place. – 463035818_is_not_an_ai Aug 19 '20 at 15:13
  • Was exploring the use of "volatile ", "auto" and "extern". So, this was just a test file, for using 'extern'. Of course, we can ignore the 'extern', as it solves the issue, but I want to know the reason for failure. – saien Aug 19 '20 at 16:15
  • The purpose of extern is to tell the compiler that you will define the variable somewhere **extern**ally to the file. But you haven't defined the variable anywhere else, so you're lying to the compiler; of course it's upset. – Boann Aug 20 '20 at 13:30

2 Answers2

5
extern int x;

tells the compiler: "I will provide you a int x in some other compilation unit". Please expect to find it at link time.

So, you need another file:

example8b.cpp

int x = 0;
int y = 0;
int z = 0;

and you need to link both files in your project.

But most importantly:

What made you choose to put x, y and z as extern in the first place ?

To end the discussion below:

  • extern int x; means "There will be an x somewhere"
  • int x; means "please put an x here"
  • undefined reference to x means "I did not find where you wanted x to be"

The compiler needs a place to put your x. You did not give it such place, because extern specifically asks the compiler not to put x there. The error is the compiler telling you to put x somewhere.

Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • So, you need another file:: So, we can't define and declare variables with 'extern' in the same file ? – saien Aug 19 '20 at 15:59
  • 3
    @SaikatGuha What is "extern" short for? – General Grievance Aug 19 '20 at 16:02
  • What made you choose to put x, y and z as extern in the first place ? :: I saw it in a user code. So was exploring the use of "volatile ", "auto" and "extern". So, this was just a test file, for using 'extern'. – saien Aug 19 '20 at 16:06
  • @Calculuswhiz : 'extern' is used to globally defining a variable. – saien Aug 19 '20 at 16:21
  • 1
    @SaikatGuha Sorry, no, that's not what `extern` means (*hint: **extern**al*). I'm not sure where you're getting that. Read [this](https://www.geeksforgeeks.org/understanding-extern-keyword-in-c/). It does a pretty good job explaining it. – General Grievance Aug 19 '20 at 17:09
  • @Calculuswhiz. thanks for the article. I understood the definition and declaration. But my doubt is, why I am not able to declare and use it in the same file. Is there some restriction for the use of ''extern''? Are"extern" variables can be declared only in an external file? – saien Aug 19 '20 at 17:18
  • @SaikatGuha No, they don't *have* to be. As mentioned in the article, you *can* assign and use `extern` on the same line (gcc grumbles) or even use `int x=0;` later on in the same file, but then the `extern` is superfluous. The purpose of `extern` is that you can see variables across multiple files. `extern` by itself is not enough. – General Grievance Aug 19 '20 at 17:36
  • @Calculuswhiz yes I agree with your point. But as I mentioned in the problem statement, the above code is throwing error and it is due to the use of extern(The code is just a test). I am getting an error in the code. Any suggestions why? – saien Aug 19 '20 at 18:48
  • @SaikatGuha the error you get is because there is no instance of x, y and z being linked in. I'll add simple analogy in my answer. Hopefully that should be the end of it. – Jeffrey Aug 19 '20 at 18:51
-2

The doubts are cleared. The solved problem(Code Edited)

#include<iostream>

  extern int x = 0;
  extern int y = 0;
  extern int z = 0;

int main(){
    
    x = 10;
    y = 15;
    
    z = (x>y ? x: y);
    
    std::cout<<z;
     
    return 0;
}

or

#include<iostream>

  extern int x;
  extern int y;
  extern int z;

int main(){
    
    int x = 10;
    int y = 15;
    
    int z = (x>y ? x: y);
    
    std::cout<<z;
     
    return 0;
}

Reference:
Example 1:

extern int var; 
int main(void) 
{ 
  var = 10; 
  return 0; 
} 
This program throws an error in compilation because var is declared but not defined anywhere. Essentially, the var isn’t allocated any memory. And the program is trying to change the value to 10 of a variable that doesn’t exist at all.

Example 2:

#include "somefile.h" 
extern int var; 
int main(void) 
{ 
 var = 10; 
 return 0; 
} 
Assuming that somefile.h contains the definition of var, this program will compile successfully.

Source: geeksforgeeks: Link, suggested by: @Calculuswhiz
Thanks to @Jeffrey

saien
  • 5
  • 4
  • Note that the first two blocks are not how `extern` was intended and do not solve the fundamental issue of the code. There is no practical reason to declare an external variable and define it in the same file. To use `const` as an analogy, this is like using `const_cast` to modify a variable you declared as `const` in the code beforehand. – General Grievance Aug 21 '20 at 03:05
  • @Calculuswhiz yes you are right. As mentioned before, this is just a beginner experiment code(code not used anywhere). 'extern' is used when we want to use the instance in an external file, it is intended to use only for that. – saien Aug 21 '20 at 12:27