#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    81
    Rep Power
    11

    Undefined reference for extern variables in a .h file


    I have a .h file that contains all the global varibales.
    The file is data.h and I have some variables in the file as below. I get undefined reference to all the variables when I try to create the execuatable.
    //data.h

    #include <stdio.h>
    extern short e_error;
    extern short field1;

    //main.c

    #include <stdio.h>
    #include <data.h>
    int main(){
    e_error=0;
    field1 =0;
    // main_code

    }

    gcc main.c

    main.c: undefined reference to e_error
    undefined reference to field1;

    Thanks.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Why aren't you linking in the file that contains those variables? All that extern does is tell the compiler that that variable is defined in another file and, oh BTW, here's its name and datatype. Then the linker has to link in that other file and fix the address references you've made to that external variable.

    It's easy to do that with gcc:
    gcc main.c data.c
    That will compiler both .c files and then link the resultant object files to create a.out (or a.exe if you're working with a Windows/DOS port of gcc).
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    81
    Rep Power
    11
    These are variables, so they will be pasted when I include the .h file. So, what is the need for the .c file?
    Thanks.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by lalitha
    These are variables, so they will be pasted when I include the .h file. So, what is the need for the .c file?
    Thanks.
    A variable has memory space assigned to it. All extern does is tell the compiler that somewhere there exists a variable by this name and is of this data type, but it does not create that variable nor does it allocate even a single bit of memory to that variable. So basically, you're lying to the compiler. You're telling it that you've created something for it to use when in fact you have not. And the compiler righteously and rightfully gets mad at you for doing that and complains of an undefined reference, which is correct because you have not defined those variables.

    The normal multiple-file programming model is that you write multiple .c source files, each of which contains its own functions and variables. Then you write header files associated with those source files and in those header files you list the function prototypes (declaring their existence and describing them, not the same thing as actually defining them, which is done in the .c source file) and the extern'd variables (ditto -- to declare that they exist and to describe them is not the same thing as actually creating them, which is also done in the .c source file).

    So if you extern variables in a header file, as you have done, you also need to actually create them in a source file somewhere and you must link that other source file into the build. I've already shown you how to do that in the invocation of gcc.


    PS
    Now is it possible for you to remove the keyword "extern" from your header file and then it would work. But that would be using the system in a manner it was not intended to be used. A perversion of the system, if you would. It would confuse other programmers and it would keep you from learning how to use header files in larger projects (eg, our product is built out of about 60 source files, so it is very important for us to keep everything straight in the header files). Do yourself a favor and learn the right way from the start.
    Last edited by dwise1_aol; March 23rd, 2006 at 08:46 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    81
    Rep Power
    11
    Thanks for the beautiful explanation.



    Originally Posted by dwise1_aol
    A variable has memory space assigned to it. All extern does is tell the compiler that somewhere there exists a variable by this name and is of this data type, but it does not create that variable nor does it allocate even a single bit of memory to that variable. So basically, you're lying to the compiler. You're telling it that you've created something for it to use when in fact you have not. And the compiler righteously and rightfully gets mad at you for doing that and complains of an undefined reference, which is correct because you have not defined those variables.

    The normal multiple-file programming model is that you write multiple .c source files, each of which contains its own functions and variables. Then you write header files associated with those source files and in those header files you list the function prototypes (declaring their existence and describing them, not the same thing as actually defining them, which is done in the .c source file) and the extern'd variables (ditto -- to declare that they exist and to describe them is not the same thing as actually creating them, which is also done in the .c source file).

    So if you extern variables in a header file, as you have done, you also need to actually create them in a source file somewhere and you must link that other source file into the build. I've already shown you how to do that in the invocation of gcc.


    PS
    Now is it possible for you to remove the keyword "extern" from your header file and then it would work. But that would be using the system in a manner it was not intended to be used. A perversion of the system, if you would. It would confuse other programmers and it would keep you from learning how to use header files in larger projects (eg, our product is built out of about 60 source files, so it is very important for us to keep everything straight in the header files). Do yourself a favor and learn the right way from the start.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Question A really strange question


    Hello guys:

    I have 3 files: file1.c file2.c and file3.c, file4.c as follows:
    file1.c
    char *global_ptr = NULL;

    file2.c
    global_ptr = x // some other char *

    file3.c
    extern char *global_ptr
    printf("global_ptr: %s\n", global_ptr); // It will throw out error: undefined reference to global_ptr

    but in file4.c, i used the same code:
    file4.c
    printf("global_ptr: %s\n", global_ptr); // compile and link right


    i really don't known how this happen, i defined the global variable global_ptr in file1.c ,and change its value in file2.c
    but in file3.c i want to print out the value of address whitch global_ptr pointed to, there would be linke error "undefined reference to **", while i do the same thing in file4.c. it totally right , how this happen??


    ths.
  12. #7
  13. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    I'm thinking you'll need to show the entire 4 files as well as the compilation and link commands to receive an accurate response.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,075
    Rep Power
    1802
    Originally Posted by jinkai2836
    Hello guys:.
    1. Do not hijack someone else's question thread to post your own question. Start a new thread.
    2. Do not resurrect long dead threads - this one is over 10 years old!
    3. If you do find a thread that appears to be related to your question, do read the existing answers to see if they solve your problem already! If they don't you have even less justification in hijacking it!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    2
    Rep Power
    0

    Smile OK my fault


    Originally Posted by clifford
    1. Do not hijack someone else's question thread to post your own question. Start a new thread.
    2. Do not resurrect long dead threads - this one is over 10 years old!
    3. If you do find a thread that appears to be related to your question, do read the existing answers to see if they solve your problem already! If they don't you have even less justification in hijacking it!
    OK , sorry to everyone. :o
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,091
    Rep Power
    2222
    Originally Posted by jinkai2836
    OK , sorry to everyone. :o
    So just exactly what is keeping you from starting your own thread? No longer interested in your "pressing" question?

    AFWIW, you already know what you need to post.

IMN logo majestic logo threadwatch logo seochat tools logo