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
.....
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
.....
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.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio: FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...
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.
Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are, by
definition, not smart enough to debug it.
- Brian W. Kernighan
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.
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.
Last edited by Igor Vartanov; July 8th, 2011 at 12:04 AM.
Bookmarks