Go to Post Not only does Dave like to post hints about the the new game...but now he's starting to post hints about the activities of the Mars rover. Does it ever stop?! - Alexa Stott [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 08-02-2008, 16:40
bronxbomber92 bronxbomber92 is offline
Registered User
FRC #1551 (Grapes of Wrath)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Naples
Posts: 75
bronxbomber92 is an unknown quantity at this point
Multiple Definitions

I've created a new file, where I keep all of this years code. When I compile my project I get an error saying symbol 'ReleasingBall' has multiple definitions.

Here's how my files are organized:

custom_routines.h
Code:
#ifndef _CUSTOM_ROUTINES_H_
#define _CUSTOM_ROUTINES_H_

//...
bool ReleasingBall;
//...
#endif
custom_routines.c
Code:
#include <timer.h>
#include "custom_routines.h"

// function definitions
user_routines.h
Code:
//..
#include "custom_routines.h" 

// all of my custom code is commented out, so beside the above include directive, everything is in its original state
Why am I getting this error, the include guards should be preventing this problem!
  #2   Spotlight this post!  
Unread 08-02-2008, 16:50
psy_wombats's Avatar
psy_wombats psy_wombats is offline
Registered User
AKA: A. King
FRC #0467 (Duct Tape Bandits)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Shrewsbury MA
Posts: 95
psy_wombats has a spectacular aura aboutpsy_wombats has a spectacular aura aboutpsy_wombats has a spectacular aura about
Re: Multiple Definitions

I'd think that variable would need to be declared global and then marked extern in all other files that use it. Or something like that, I forget the exact procedure.
  #3   Spotlight this post!  
Unread 08-02-2008, 16:55
Alan Anderson's Avatar
Alan Anderson Alan Anderson is offline
Software Architect
FRC #0045 (TechnoKats)
Team Role: Mentor
 
Join Date: Feb 2004
Rookie Year: 2004
Location: Kokomo, Indiana
Posts: 9,113
Alan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond reputeAlan Anderson has a reputation beyond repute
Re: Multiple Definitions

When you copy a project to a new folder, you should Clean (or Build all) to get rid of any leftover pointers to the old files. Until you do that, the linker appears to get confused, complaining about what looks like duplicate copies of the object files.

[Edit

Oh! You're not supposed to declare variables in .h files. The bool ReleasingBall; line belongs in custom_routines.c instead, and what goes in custom_routines.h should be extern bool ReleasingBall; (though I'm not sure where bool is defined).

Last edited by Alan Anderson : 08-02-2008 at 16:59. Reason: read the problem more closely
  #4   Spotlight this post!  
Unread 08-02-2008, 17:00
wireties's Avatar
wireties wireties is offline
Principal Engineer
AKA: Keith Buchanan
FRC #1296 (Full Metal Jackets)
Team Role: Mentor
 
Join Date: Jan 2006
Rookie Year: 2004
Location: Rockwall, TX
Posts: 1,170
wireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond reputewireties has a reputation beyond repute
Send a message via AIM to wireties
Re: Multiple Definitions

You want the compiler to encounter only one declaration for the variable as it processes the set of source source files. Your technique above is designed to prevent multiple references or forward references to be encountered , not multiple variables. I'm sure your custom_routines.h file is processed when you compile multiple separate source files.

So do his, change the statement in custom_routines.h to an external reference and declare storage for the variable in ONLY one source file.

In custom_routines.h, do this:

#ifndef _CUSTOM_ROUTINES_H_
#define _CUSTOM_ROUTINES_H_
...
extern bool ReleasingBall;
...
#endif

In custom_routines.c, do this:

....
#include <timer.h>
#include "custom_routines.h"

bool ReleasingBall; // defined only here!

// function definitions
...

One last thing, it is generally considered bad style to define storage in a header file for exactly the reason you have encountered.

HTH
  #5   Spotlight this post!  
Unread 08-02-2008, 17:03
bronxbomber92 bronxbomber92 is offline
Registered User
FRC #1551 (Grapes of Wrath)
Team Role: Programmer
 
Join Date: Jan 2007
Rookie Year: 2007
Location: Naples
Posts: 75
bronxbomber92 is an unknown quantity at this point
Re: Multiple Definitions

Quote:
Originally Posted by Alan Anderson View Post
When you copy a project to a new folder, you should Clean (or Build all) to get rid of any leftover pointers to the old files. Until you do that, the linker appears to get confused, complaining about what looks like duplicate copies of the object files.

[Edit

Oh! You're not supposed to declare variables in .h files. The bool ReleasingBall; line belongs in custom_routines.c instead, and what goes in custom_routines.h should be extern bool ReleasingBall; (though I'm not sure where bool is defined).
Thanks, worked great!

I defined bool my self with a few typedefs and defines
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Short definitions of overdrive HedlessChkn General Forum 3 13-01-2008 21:52
Hosting multiple sites in multiple VMs EHaskins Website Design/Showcase 6 22-12-2007 01:09
Error - symbol 'command_list' has multiple definitions. Moloch Programming 2 17-02-2005 17:32
Error - symbol 'e' has multiple definitions. HELLPPPPP BookerT Programming 2 24-01-2005 21:50


All times are GMT -5. The time now is 03:28.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi