View Single Post
  #2   Spotlight this post!  
Unread 14-02-2008, 13:14
Mark McLeod's Avatar
Mark McLeod Mark McLeod is online now
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,854
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Interesting Problems with the Compiler and Linker

Never declare variables in header files (.h), otherwise, everytime you include that header file you'll be ordering the compiler to declare the variable all over again. That's why you see those "multiply defined" errors.

In the header file use:
Code:
extern char myvariable;
Then really declare the variable as a global only in one of your .c files.

There is an alternative way to declare variables in a header file that involves a conditional compile:
Code:
#ifdef DECLARE_MY_VARIABLES
    char myvariable;
#else
    extern char myvariable;
#endif
And in one (and only one) .c file
Code:
#define DECLARE_MY_VARIABLES
    #include that_header_file.h
#undef DECLARE_MY_VARIABLES
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle

Last edited by Mark McLeod : 14-02-2008 at 14:01.