Hi again. New problem. I creaated a new project that only has main.c file1.c and file1.h
file1.h is #included in main.c which contains a global variable.
When I build all I get :- .\scope.axf: Error: L6200E: Symbol global_VAR multiply defined (by main.o and file.o).
How do I get round this?
I have googled around but don't quite understand the issue.
Start by showing us what your code looks like.
But I guess you forgot the "extern" keyword in the header file, to just inform the compiler that there "somewhere" exists a variable with that name and type.
Don't mix up declaration and definition.
ok, the files are as such :-
****main.c****
#include "stm32f4xx.h" #include "file1.h"
int main(void) { SystemInit();
testFUNC();
while(1) {
} } ********************************************************* ****file1.c****
#include "file1.h"
void testFUNC(void) { VAR++; } ********************************************************* ****file1.h****
#ifndef __file1_H #define __file1_H
#include "stm32f4xx.h"
void testFUNC(void); volatile unsigned int VAR;
#endif
Don't put definitions in the include files.
foo.c
volatile unsigned int VAR;
foo.h
extern volatile unsigned int VAR;
If you "define" it in multiple places, the linker is going to righteously *** about it.
Thanks again. It worked. Seems strange though as other IDE iv'e used never seemed to moan about.
Are "header" treated differently? Reason I ask is because when I try and add Header files they are referenced as "text" files by the IDE's file types filter.
First off - separate IDE from the tool chain that is actually compiling/linking your project.
It's the C standard that complains about having the same variable in multiple source [*.c] files. But some linkers are able to just grumble a bit and assume that two symbols of same name and size probably should be the same variable. So the linker merges the variables. But that isn't part of the C standard - it was basically a spin-off from how linkers are required to handle some constructs (like templates and inline functions) in C++.
But that isn't part of the C standard - it was basically a spin-off from how linkers are required to handle some constructs (like templates and inline functions) in C++.
Erm, no. C++ had nothing to do with that. Merging common definitions occuring in multiple modules is a feature that predates C++ by at least a decade. It was common in Unix linkers, because those had to be able to link not only C programs, but also FORTRAN programs, and programs built from a mix of FORTRAN and C modules, too. Even the official C standard mentions this behaviour as a "common extension" (C99 J.5.11).
The reason for this was that FORTRAN had only a very weak equivalent of a global variable definition, which also had to do double duty as the only kind of data structure it supported besides arrays: the named COMMON block. Since the there was no distinction between definition and declaration, those named COMMONs had to be defined by every module that used them, and it was then up to the linker to merge these repeated definitions.
Some C++ tool chains may have re-used this functionality for inlines and template instantiations (before they came to their senses and did it properly, using weak symbols). But it existed a long time before C++ did.