After 2 years of (happy) idle time, I find myself in the necessity of migrating my old OpenAT projects (based on VS6) to this new Eclipse-based environment. I have solved all the issues related to changes in OpenAT libraries, and all my c files now compile. The problem arises when linking:
For every variable I declare in my header files I recieve the error message multiple definitions of …
I have read the thread [url]Migrating to M2M Studio from VS 2008] and came up with the explanation:
Well, in my header files, all variables are simply declared, never defined.
By some reason I was not allowed to include a sample code in the previous post. Here is a sample of the lines in my headers that are giving me such a ‘headache’.
That way should look your .c file, not the header. You should have: //xx.c
s8 hData=0;
bool bPortReady=FALSE;
bool bLastReadingOK=FALSE;
bool bProgramPaused=FALSE;
That’s not the case. I’ve even commented out any line that used some of such variables, so in fact the only reference across my project to those variables is their declaration in the .h file, and still is marked as “multiple-declared”.
… my code are declarations (not definitions, the variables are not initialized). On the other hand, if you insist on my sample code being definitions, then: how should I declare a shared variable?
An initialisation is sufficient to make a definition - but it is not necessary.
As Flex told you, your header file should contain just the extern declarations; eg,
If you think about the purpose of header files, this should be obvious: the header is telling the compiler that these variables will be defined somewhere externally to the current compilation unit.
You must then provide definitions in exactly one of your ‘C’ source files; eg,
The definition is the thing which actually causes resources to be committed
I do - as does the definition of the ‘C’ programming language!
1.- Developer Studio is using a C++ compiler to compile my C files. Can that be changed?
2.- It is not enough to correct the .h files: the GUI is so “clever” that it won’t mark affected .o files as outdated, so changes made to .h files will have no impact on final result.
Thanks for your patience, awneil; in my old C-way thinking, “extern int a;” and “int a;” at file-scope are equivalent.
Too verbose I think. It is shorter to use hard coded ‘extern’ in headers and definitions in .c files. And much better not to use global variables at all.