2

I am having a lot of issue using extern variable and header files. I have read through sections of books and searched the web for hours but I haven't been able to figure out. Any help in understanding this problem will be greatly appreciated. The following is the code and the error when I try to compile

    #include <stdio.h>
    #include <stdlib.h> 
    #include <string.h>
    #include "sample.h"

    int main()
      {
          int i;
          int gI = 0;
          double recv;

          i = 10;
          gI = i;

          recv = AnotherFunc();

          return 0;
      }

And the sample.h is the following

      #ifndef SAMPLE
      #define SAMPLE

      extern int gI;
      extern double AnotherFunc(); 

      #endif

And the other is function is

       #include <math.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <string.h>
       #include "sample.h"

       double AnotherFunc()
        {
           double someVar;
           int test;

           test = gI;

           someVar = 10.0;  
           return someVar;
         }

When I compile the following way, I get the following errors which I don't understand why I am getting those errors. sample.h has the variable declaration and it should be visible in the AnotherFunc.

          gcc -Wall -ansi -pedantic -c Main.c AnotherFunc.c
          gcc Main.o AnotherFunc.o -o test
          AnotherFunc.o: In function `AnotherFunc':
          AnotherFunc.c:(.text+0x6): undefined reference to `gI'
          collect2: ld returned 1 exit status

I only added the int gI = 0; because I wanted to define it. If I modify the code in the following way I get error in main also. Please see below.

           #include <stdio.h>
           #include <stdlib.h>
           #include <string.h>
           #include "sample.h"

           int main(int argc, char *argv[])
            {
              int i;
              double recv;

               i = 10;
              gI = i;

              recv = AnotherFunc();

              return 0;
            }

             gcc -Wall -Wstrict-prototypes -ansi -pedantic -c Main.c AnotherFunc.c
             gcc Main.o AnotherFunc.o -o test
             Main.o: In function `main':
             Main.c:(.text+0x1b): undefined reference to `gI'
             AnotherFunc.o: In function `AnotherFunc':
             AnotherFunc.c:(.text+0x6): undefined reference to `gI'
             collect2: ld returned 1 exit status
user3357091
  • 31
  • 1
  • 3
  • You have defined `gI` as local to `main()` - you need to declare it outside of `main()`, at global level. – Andrew Jul 12 '16 at 06:05

5 Answers5

3

Move int gI = 0 outside main() so that it is globally available:

int gI = 0; 
int main()
  {
      int i;

  ....
  } 
Manoj Awasthi
  • 3,460
  • 2
  • 22
  • 26
2

You need to declare your variables before using them, and define them exactly once.

This is a declaration:

extern int gI;

Basically this just says that there is an int named gI that will be defined elsewhere.

This is a definition:

int gI;

This actually creates an int named gI. (Technically it is a declaration as well as a definition.)

At the moment you have an int gI line inside of your main function, but this is just a shadowing definition. It is a local variable whose name happens to be the same as the declared global gI, but it is not the global gI. So you have a problem where you declared a variable (the global gI) and defined it zero times.

If you were to put int gI in your sample.h file, it would then be included in both of your .c files. This would also be a problem because the rule is to define variables exactly once, and you will have defined it twice (once in each file).

The solution is to place the extern declaration in the .h file, and the definition in one of your .c files.

Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
1

An external variable must be defined, exactly once, outside of any function; this sets aside storage for it. The variable must also be declared in each function that wants to access it;

check this link. well explained How do I use extern to share variables between source files?

Community
  • 1
  • 1
tesseract
  • 891
  • 1
  • 7
  • 16
  • I understand and that's why I included the sample.h in AnotherFunc. But I get the error saying undefined reference – user3357091 Feb 26 '14 at 18:25
  • extern keyword declares a variable, but doesn't define it, you should define the variable in 1 .c file and use the extern in the header. – tesseract Feb 26 '14 at 18:48
0

You defined gI inside the scope of the main() function, which makes it visible only from there. I suspect what you really wanted is a global gI variable (hence the extern int gI declaration).

If you want to have AnotherFunc() see it, move int gI = 0 outside, for instance in the same file than the definition of AnotherFunc().

Albatropy
  • 13
  • 3
  • Wouldn't including sample.h serve the purpose of declaring a variable outside the scope of main. Please look at the following code and error when I compile – user3357091 Feb 26 '14 at 18:19
0

gI must be declared above main:

int gI = 0;

int main(void)
{
    ...
}

By doing this gI has file scope and external linkage.

Perhaps a better place to declare gI would be in sample.c, if there is such a file.

Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46