March 23rd, 2006, 07:22 PM
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.
extern short e_error;
extern short field1;
main.c: undefined reference to e_error
undefined reference to field1;
March 23rd, 2006, 07:40 PM
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).
March 23rd, 2006, 08:42 PM
These are variables, so they will be pasted when I include the .h file. So, what is the need for the .c file?
March 23rd, 2006, 09:31 PM
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.
Originally Posted by lalitha
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.
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 09:46 PM.
March 23rd, 2006, 10:12 PM
Thanks for the beautiful explanation.
Originally Posted by dwise1_aol
January 13th, 2014, 05:33 AM
A really strange question
I have 3 files: file1.c file2.c and file3.c, file4.c as follows:
char *global_ptr = NULL;
global_ptr = x // some other char *
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:
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??
January 13th, 2014, 09:35 AM
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] are essential for python code and Makefiles!
January 13th, 2014, 02:16 PM
Originally Posted by jinkai2836
- Do not hijack someone else's question thread to post your own question. Start a new thread.
- Do not resurrect long dead threads - this one is over 10 years old!
- 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!
January 13th, 2014, 08:11 PM
OK my fault
OK , sorry to everyone. :o
Originally Posted by clifford
January 14th, 2014, 01:46 AM
So just exactly what is keeping you from starting your own thread? No longer interested in your "pressing" question?
Originally Posted by jinkai2836
AFWIW, you already know what you need to post.