Go to Post The problem was too many penalties with too much room for judgment. - dbSparx [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 07-02-2004, 11:16
Darkman_X000 Darkman_X000 is offline
Registered User
#0612
 
Join Date: Feb 2004
Location: Chantilly, VA
Posts: 8
Darkman_X000 is an unknown quantity at this point
Compiler Help Needed

Ok guys, what is the deal!? I am getting the following errors when I compile:
Code:
OUTPUT
MPLINK 3.40, Linker
Copyright (c) 2003 Microchip Technology Inc.
Error - symbol 'axelWidth' has multiple definitions.
Errors    : 1

BUILD FAILED: Sat Feb 07 11:04:01 2004
This is really annoying because it does not tell me where the other definitions are. In C++, and in this compiler, if I define the same thing twice explicitly, it tells me where the previous definition is. Here is a sample of my code:
Code:
UserUtilities.h
#ifndef USERUTILITIES_H
#define USERUTILITIES_H

#include "bool.h"

float axelWidth=.725;
float wheelRadius=.105;
int clicksPerRevolution=16;

bool newValue(int *staticVar, int dynamicVar);
float thetaDegrees(int c);
float thetaRadians(int c);
float Forward(int c);

#endif

UserUtilities.c
#ifndef USERUTILITIES_C
#define USERUTILITIES_C

#include "bool.h"
#include "UserUtilities.h"

bool newValue(int *staticVar, int dynamicVar)
{
	
	if (*staticVar==dynamicVar)
	{
		*staticVar=dynamicVar;
		return true;
	}
	else
	{	
		return false;
	}
}

float thetaDegrees(int clicks)
{
	extern float axelWidth;
	extern float wheelRadius;
	clicks*=22.5;
	clicks=clicks*wheelRadius;
	clicks/=(float)axelWidth;
	return clicks; //22.5*(float)wheelRadius*clicks/(float)axelWidth;
}

float thetaRadians(int clicks)
{
	extern float axelWidth;
	extern float wheelRadius;
	clicks*=3.1415926;
	clicks=clicks*wheelRadius;
	clicks/=8*axelWidth;
	return clicks;
}

#endif
Note: these are the only files that contain the definitions of these functions and varibles. There are NO duplicate names or any of that.
WHAT IS GOING ON!?!?!?!?!?!?!?!?!?
Clearly, including the file multiple times is prevented by the preprocessor #ifndef, #define, #endif statements, but it is acting like it just disregards those...
  #2   Spotlight this post!  
Unread 07-02-2004, 11:29
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Compiler Help Needed

Slow down and look at what you are doing.

You defined axelWidth in userutilities.h and then included userutilities.h in userutilities.c... This means that axelWidth is defined as a float.

Now you redefine axelWidth as an external variable in userutilities.c meaning that some OTHER module has defined axelWidth. This is a double declaration and is only the tip of the iceberg... I see a bunch. i.e. you include bool.h multiple times...

Also, take a few minutes and see various threads about float variables and implicit initializations... You are going down a very dangerous road.

Regards,
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #3   Spotlight this post!  
Unread 07-02-2004, 11:31
Mike Soukup's Avatar
Mike Soukup Mike Soukup is offline
Software guy
FRC #0111 (Wildstang)
Team Role: Engineer
 
Join Date: May 2001
Rookie Year: 1996
Location: Schaumburg, IL
Posts: 797
Mike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond reputeMike Soukup has a reputation beyond repute
Re: Compiler Help Needed

Quote:
Originally Posted by Darkman_X000
Note: these are the only files that contain the definitions of these functions and varibles. There are NO duplicate names or any of that.
WHAT IS GOING ON!?!?!?!?!?!?!?!?!?
Clearly, including the file multiple times is prevented by the preprocessor #ifndef, #define, #endif statements, but it is acting like it just disregards those...
The problem is actually with the line "extern float axleWidth". Since you're including the header that has the declaration of "axleWidth" there is no need to re-declare it inside your function. 'extern' is only used when you have a global variable defined in another file.

From the names of your variables & the way you use them, it looks like those are values that will never change while your code is being executed, ie a constant. If so, there is no need to use a variable to store the values, use a #define instead:

Code:
#define axelWidth  .725
#define wheelRadius  .105
#define clicksPerRevolution  16
Post more questions if you have them.
  #4   Spotlight this post!  
Unread 07-02-2004, 12:15
Darkman_X000 Darkman_X000 is offline
Registered User
#0612
 
Join Date: Feb 2004
Location: Chantilly, VA
Posts: 8
Darkman_X000 is an unknown quantity at this point
Re: Compiler Help Needed

Ok, I just replaced what I had before with:
Code:
#define axelWidth  .725
#define wheelRadius  .105
#define clicksPerRevolution  16
and I have deleted the externs. Now the compiler has no trouble with "axelWidth". Instead it says:
Code:
MPLINK 3.40, Linker
Copyright (c) 2003 Microchip Technology Inc.
Error - symbol 'newValue' has multiple definitions.
Errors    : 1

BUILD FAILED: Sat Feb 07 12:05:03 2004
I wrote the function prototype in UserUtilities.h and the actual function definition in UserUtilities.c. There should be no conflicts here because prototypes ARE allowed.

Each time I fix a problem with one function or variable name in the set of files, it has a problem with the next one down the line.

Please consider that because bool.h has the necessary preprocessor statements, including it multiple times should not be a problem... right? Isn't that the primary use of #ifndef, #define, and #endif structures in header files?

I also tried to delete the #include "userutilities.h" line from UserUtilities.c after making the above changes, but then it said that axelWidth and wheelRadius were not defined. Can I use extern with a variable that was #defined?

Here is the updated version of my files:
Code:
UserUtilities.h
#ifndef USERUTILITIES_H
#define USERUTILITIES_H

#include "bool.h"

#define axelWidth .725
#define wheelRadius .105
#define clicksPerRevolution 16

/*float axelWidth = .725;
float wheelRadius = .105;
int clicksPerRevolution = 16;*/

bool newValue(int *staticVar, int dynamicVar);
float thetaDegrees(int c);
float thetaRadians(int c);
float Forward(int c);
void move(float distance);
void turnLeft(float degrees);
void turnRight(float degrees);

#endif

UserUtilities.c
#ifndef USERUTILITIES_C
#define USERUTILITIES_C

#include "bool.h"
#include "UserUtilities.h"

bool newValue(int *staticVar, int dynamicVar)
{
	
	if (*staticVar==dynamicVar)
	{
		*staticVar=dynamicVar;
		return true;
	}
	else
	{	
		return false;
	}
}

float thetaDegrees(int clicks)
{
	clicks*=22.5;
	clicks=clicks*wheelRadius;
	clicks/=(float)axelWidth;
	return clicks; //22.5*(float)wheelRadius*clicks/(float)axelWidth;
}

float thetaRadians(int clicks)
{
	clicks*=3.1415926;
	clicks=clicks*wheelRadius;
	clicks/=8*axelWidth;
	return clicks;
}

#endif
  #5   Spotlight this post!  
Unread 07-02-2004, 12:26
Darkman_X000 Darkman_X000 is offline
Registered User
#0612
 
Join Date: Feb 2004
Location: Chantilly, VA
Posts: 8
Darkman_X000 is an unknown quantity at this point
Re: Compiler Help Needed

Quote:
Originally Posted by Mike Betts
Take a few minutes and see various threads about float variables and implicit initializations... You are going down a very dangerous road.
So, after the prescribed research, I still do not understand the "dangerous road" of floats and implicit initializations. Please enlighten me.

Thanx in advance.
  #6   Spotlight this post!  
Unread 07-02-2004, 17:45
Random Dude Random Dude is offline
Oregon State Head FTA
AKA: Chris
no team (Oregon Robotics Tournament & Outreach Program)
 
Join Date: Aug 2002
Rookie Year: 1998
Location: Oregon
Posts: 142
Random Dude will become famous soon enoughRandom Dude will become famous soon enough
Re: Compiler Help Needed

Quote:
Originally Posted by Darkman_X000
So, after the prescribed research, I still do not understand the "dangerous road" of floats and implicit initializations. Please enlighten me.

Thanx in advance.

Well, the issue with floats is that this micro does not have a Floating Point Unit. So, while on your PC the FPU would quicky preform any math involving floats, that all has to be emulated in software on this micro. That is bad becuase that emulation is fairly complex and could potentially slow down your program considerably, especially if you use alot of floating point numbers.

The concerns about the implicit initializations, is basically that normally on microprocessors, the line

int i = 5;

may not automatically initalize i = 5. Though that is really a moot point in this case since IFI has a routine that does initalize the variables for us. (in ifi_startup.c for those who haven't seen it.)


-----------------------
No, you can't use extern with #define. #define isn't actually a variable, what it means, is before the code is complied (in the pre-processor), the any time the first item after the #define is found, it is replaced with the second. So:

#define PI 3.1415926538
int C, R = 3;
C= 2*PI*R;


Becomes (after the pre-processor, but before the actuall compiler)

int C, R = 3;
C= 2*3.1415926538*R;



So if you just put the

#define axelWidth .725

in one header file, that you include everywhere, you can use axelWidth with no other effort.


------------------------------
  #7   Spotlight this post!  
Unread 07-02-2004, 19:10
Jeff McCune's Avatar
Jeff McCune Jeff McCune is offline
Alpha Geek
#0677 (The Wirestrippers)
Team Role: Mentor
 
Join Date: Jan 2003
Location: The Ohio State University
Posts: 67
Jeff McCune is on a distinguished road
Send a message via ICQ to Jeff McCune Send a message via AIM to Jeff McCune
Re: Compiler Help Needed

As an example, when using a microcontroller, using this:

#define PI (22 / 7)

is *considerably* faster than using 3.14159... and it gives a fairly decent approximation.

Also, to prevent re-inclusion of header files, people usually do something like this:

myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H

.. Entire header file ...

#endif
/* ifndef MYHEADER_H */

This prevents weird recursion problems from having a header file included twice at the top of a C file.

It's sloppy, and I don't recommend doing this, because if you *need* to do this, you're already down the dangerous road they've been speaking of and you ultimately need to fix that or you'll just have a *monstrous* headache at competition when you're rapidly changing code.
__________________
Team 677 - The Wirestrippers - Columbus School for Girls and The Ohio State University
EMAIL: mccune@ling.ohio-state.edu

...And all you touch and all you see
Is all your life will ever be...
  #8   Spotlight this post!  
Unread 07-02-2004, 19:19
Jeff McCune's Avatar
Jeff McCune Jeff McCune is offline
Alpha Geek
#0677 (The Wirestrippers)
Team Role: Mentor
 
Join Date: Jan 2003
Location: The Ohio State University
Posts: 67
Jeff McCune is on a distinguished road
Send a message via ICQ to Jeff McCune Send a message via AIM to Jeff McCune
Re: Compiler Help Needed

Also, you can completley eliminate floating point from your code.

#define axelWidth 0.725

becomes:

#define AXEL_WIDTH_NUM 29
#define AXEL_WIDTH_DEN 40

Constants that are #defined are usually all caps by convention.

Also, keep in mind that when using integers, order of operations isn't the same as it is in your math class. Well, it is, but the order of operations will affect the result of the equation.
Code:
// Consider:
#define NUM 50
#define DEN 100
int foo = NUM / DEN * joy1_y
int bar = NUM * joy1_y / DEN
foo will ALWAYS be 0. Always. this is becuase NUM divided by DEN in integer math will be 0, and 0 multiplied by anything is zero. bar will contain what you think it should based on the equation.

Basically, stick to integer math, and multiply to the biggest number you can before starting to divide using integers to prevent always multoplying by zero.
__________________
Team 677 - The Wirestrippers - Columbus School for Girls and The Ohio State University
EMAIL: mccune@ling.ohio-state.edu

...And all you touch and all you see
Is all your life will ever be...

Last edited by Jeff McCune : 07-02-2004 at 19:22.
  #9   Spotlight this post!  
Unread 07-02-2004, 20:11
josh_johnson josh_johnson is offline
Registered User
#1020 (Indiana Prankmonkeys)
 
Join Date: Nov 2002
Location: Muncie, IN
Posts: 58
josh_johnson is an unknown quantity at this point
Send a message via AIM to josh_johnson Send a message via Yahoo to josh_johnson
Re: Compiler Help Needed

Quote:
Originally Posted by Jeff McCune
Basically, stick to integer math, and multiply to the biggest number you can before starting to divide using integers to prevent always multoplying by zero.
This can be a dangerous aproach, as often the largest value is too large for the variable type that you are putting it in, but the final result would fit just fine. Just make sure that any intermediate values are not going to be too large before doing this.
__________________
5! * (4! - 3! - 1!) / 2!
  #10   Spotlight this post!  
Unread 07-02-2004, 21:08
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Compiler Help Needed

Quote:
Originally Posted by Mike Soukup
... From the names of your variables & the way you use them, it looks like those are values that will never change while your code is being executed, ie a constant. If so, there is no need to use a variable to store the values, use a #define instead:

Code:
#define axelWidth  .725
#define wheelRadius  .105
#define clicksPerRevolution  16
...
I, personally, have had problems with #declare in place of const, and only use it for alias, Example:
Code:
//Some constants
const char Varience = 10;
const char True = 255;
const char bLAH = 152;

//Some aliases
#declare Left_OI p1_y
#declare Left_Motor pwm01
#declare DyNoMiTe relay8
As long as you keep it in headers and include them, you should be ok.
  #11   Spotlight this post!  
Unread 07-02-2004, 21:48
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Compiler Help Needed

first of all, it's "#define", not "#declare"

second, the reason you are having problems is because mcc18 automatically casts any number from 0 to 255 as an unsigned char, which can screw up math. instead, what you'd want is this:

#define Varience ((char) 10)
__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #12   Spotlight this post!  
Unread 08-02-2004, 02:05
Random Dude Random Dude is offline
Oregon State Head FTA
AKA: Chris
no team (Oregon Robotics Tournament & Outreach Program)
 
Join Date: Aug 2002
Rookie Year: 1998
Location: Oregon
Posts: 142
Random Dude will become famous soon enoughRandom Dude will become famous soon enough
Re: Compiler Help Needed

Quote:
Originally Posted by Jeff McCune
As an example, when using a microcontroller, using this:

#define PI (22 / 7)

is *considerably* faster than using 3.14159... and it gives a fairly decent approximation.
Umm.. But (22/7) evaluates to (3).
  #13   Spotlight this post!  
Unread 08-02-2004, 19:02
Astronouth7303's Avatar
Astronouth7303 Astronouth7303 is offline
Why did I come back?
AKA: Jamie Bliss
FRC #4967 (That ONE Team)
Team Role: Mentor
 
Join Date: Jan 2004
Rookie Year: 2004
Location: Grand Rapids, MI
Posts: 2,071
Astronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud ofAstronouth7303 has much to be proud of
Re: Compiler Help Needed

Only in integer math (pi rounded down is three).

Ps- My bad on the #declare!
  #14   Spotlight this post!  
Unread 08-02-2004, 19:28
deltacoder1020's Avatar
deltacoder1020 deltacoder1020 is offline
Computer Guy
AKA: Dav
#1020 (The Indiana Prank Monkeys)
Team Role: Programmer
 
Join Date: Jan 2004
Location: Muncie, Indiana
Posts: 340
deltacoder1020 has a spectacular aura aboutdeltacoder1020 has a spectacular aura about
Send a message via AIM to deltacoder1020
Re: Compiler Help Needed

Quote:
Originally Posted by Random Dude
Umm.. But (22/7) evaluates to (3).
technically, 3 is a "decent approximation" for pi... but heck, if you're gonna do that, just do what some crazy state legislature tried to do, and

#define PI 3

__________________
Team 1020, the Indiana Prank Monkeys (www.team1020.org)
  #15   Spotlight this post!  
Unread 08-02-2004, 20:44
Random Dude Random Dude is offline
Oregon State Head FTA
AKA: Chris
no team (Oregon Robotics Tournament & Outreach Program)
 
Join Date: Aug 2002
Rookie Year: 1998
Location: Oregon
Posts: 142
Random Dude will become famous soon enoughRandom Dude will become famous soon enough
Re: Compiler Help Needed

Quote:
Originally Posted by Astronouth7303
Only in integer math (pi rounded down is three).

Ps- My bad on the #declare!

Yes, exactly (22/7) is integer math (22.0/7) would be floating point. [Or ((float)22/7)]
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
C compiler for 2004 Tim Skloss Technical Discussion 11 05-11-2003 22:02
C compiler for 2004 Tim Skloss Robotics Education and Curriculum 2 04-11-2003 16:19
The Grand FIRST team.. programmers and others needed randomperson Programming 0 31-05-2003 23:46
Weight needed to tilt bridge... archiver 2001 4 23-06-2002 23:43
Inventor 5.0 help needed Larry Barello Inventor 4 27-01-2002 10:54


All times are GMT -5. The time now is 22:47.

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