error LNK2001: unresolved external symbol
I am included C headers file in my VC8 project. In one of my C header file , we have few definistions of the variables. When i compile my project it gives me linker error for all the global variables.
My global hearder file "intdefs.h" has below data:
long rfl_xref_id;
I have included intdefs.h file in my main cpp file say myfile.cpp:
#include intdefs.h
.........
extern long rfl_xref_id
.....
getting linker error :error LNK2001: unresolved external symbol "long rfl_xref_id" (?rfl_xref_id@@3JA)
Re: error LNK2001: unresolved external symbol
Quote:
Originally Posted by
annieshaw
I am included C headers file in my VC8 project. In one of my C header file , we have few definistions of the variables. When i compile my project it gives me linker error for all the global variables.
My global hearder file "intdefs.h" has below data:
long rfl_xref_id;
I have included intdefs.h file in my main cpp file say myfile.cpp:
#include intdefs.h
.........
extern long rfl_xref_id
.....
getting linker error :error LNK2001: unresolved external symbol "long rfl_xref_id" (?rfl_xref_id@@3JA)
Could you please post your intdefs.h file?
Are your variables defined in a global namespace?
Are there conditional preprocessor directives?
Also, it is a bad idea to have variable definition in the header file, you should only have declarations there.
Re: error LNK2001: unresolved external symbol
As Vladimir say you have to have a long rfl_xref_id; in one of your source files.
extern long rfl_xref_id tell the compiler that no space has to be allocated for this variable since it's located in some other source file.
Re: error LNK2001: unresolved external symbol
Its solved. I was declared and define in the same file(as per C programing concept). In C++, declartion goes to header file and definitions should be in CPP file.
Thanks for you help!
Re: error LNK2001: unresolved external symbol
Quote:
I was declared and define in the same file(as per C programing concept). In C++, declartion goes to header file and definitions should be in CPP file.
Actually, C and C++ share the same concept. As per design, C++ was made a successor to C. :)
Header files contain declarations with external linkage (functions are automatically given the one), while compile units (.c or .cpp) contain definitions. Without external declaration a variable is limited to unit scope, compiler generates allocation code for the variable, and its references appear resolved immediately in the unit scope. External declaration instructs to resolve references in the other units (link them to variable real location) on a later stage by linker. That's why linker is called linker. ;)