Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jul 12, 2007, 12:23 AM   #1
guest42
macrumors newbie
 
Join Date: Jul 2007
XCode: Multiple Definitions of Symbol

Hey all. I am having an infuriating problem with my code. About a month ago I began to program a game in C with openGL, and XCode was great for getting started. However I stopped working on it for weeks until this morning, and for some reason it suddenly is not wanting to compile, giving me a 'multiple definitions' error for a constant float in on of my header files, even though I did a #ifndef-#define-#endif thing.

In a simplest case scenario, the project is a Carbon Application with three classes: main.c, util.h, and util.c, that look like the following:

main.c:

Code:
#include "futil.h"

int main(void) {
     return 0;
}
util.h:

Code:
#ifndef TEST
#define TEST

const float mat = 100.8/45.0; //it tries to define this twice, I guess

#endif
util.c:

Code:
#include "util.h"

And that's it. It just doesn't like that constant float, when it didn't have a problem with it a month ago. The error looks something like:
Code:
multiple definitions of _mat
If I remove util.c's include, and it's only included once, it works.

I tried fixing this by taking out the constant and code that uses it, but then it came up with a bunch of nonsensical 'undefined' errors for seemingly random things. I even tried installing an older version of XCode, and then in frustration I reinstalled the entire OS thinking that some update had effed things up, but it's doing the same thing.

So on one hand, that tells me it's the code, but on the other hand, it compiled last month just fine, and those compiler directives seem like they ought to keep that variable from being defined twice. Am I missing something! I'd really like to get back to making this game, and I'm crushed that the code isn't working now. Any help would be deeply appreciated.
guest42 is offline   0 Reply With Quote
Old Jul 12, 2007, 12:55 AM   #2
HiRez
macrumors 601
 
HiRez's Avatar
 
Join Date: Jan 2004
Location: Western US
What happens if you change the name "mat" to something else? Or if you do:

Code:
#define MAT 100.8/45.0
__________________
Go outside, the graphics are amazing!
HiRez is offline   0 Reply With Quote
Old Jul 12, 2007, 01:32 AM   #3
guest42
Thread Starter
macrumors newbie
 
Join Date: Jul 2007
Well, I did that, and it worked! It turns out the undefined errors it picked up afterwards were indeed my own doing. What I want to know though is why it would have compiled before if it doesn't like constants declared in that manner. I can only suppose that previously I had never cleaned my work space, and that somehow screwed things up, but I am only speculating. I suppose from now on if I want a constant variable I'll just define it with the compiler.

But could you explain the nature of constants for me, and how they could cause this problem?

In any case, thanks for your help
guest42 is offline   0 Reply With Quote
Old Jul 12, 2007, 04:34 AM   #4
GothicChess.Com
macrumors regular
 
Join Date: Apr 2007
Location: Philadelphia, PA
Send a message via AIM to GothicChess.Com
Quote:
Originally Posted by guest42 View Post
Well, I did that, and it worked! It turns out the undefined errors it picked up afterwards were indeed my own doing. What I want to know though is why it would have compiled before if it doesn't like constants declared in that manner. I can only suppose that previously I had never cleaned my work space, and that somehow screwed things up, but I am only speculating. I suppose from now on if I want a constant variable I'll just define it with the compiler.

But could you explain the nature of constants for me, and how they could cause this problem?

In any case, thanks for your help
You defined it as a constant, yet you executed a formula. It is best to either:

A. Make it a variable.
B. Compute the constant, then declare it as its explicit value. That is, don't trust the compiler
C. Use a #define to creat a pre-compiled instance of it, which basically overrides whatever will come next in the code.
__________________
World's Only Dual-Processor G4 Cube (2x500 MHz) 1989 Macintosh SE/30 @ 32 MHz is still kicking 1st Mac was purchased Friday, October 3, 1984
GothicChess.Com is offline   0 Reply With Quote
Old Jul 12, 2007, 06:18 AM   #5
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
Hi
Did you switch your build from development to release?

You are actually defining multiple definitions of mat. Every time a compilation unit includes util.h it is declaring the variable mat. So in your example, main.c and util.c both declare the variable mat hence the error.

The #defines do guard against multiple inclusions but not across compilation units. For example, if main.c inludes main.h and util.h, and in addition main.h also includes util.h, then the #defines stop util.h being processed twice. This is different from the case where main.c and util.c both include util.h.

In general I think it is much better practise to use const instead of #defines.

b e n
lazydog is offline   0 Reply With Quote
Old Jul 12, 2007, 06:41 AM   #6
iSee
macrumors 68030
 
iSee's Avatar
 
Join Date: Oct 2004
yeah, lazydog is right.

Your header should declare but not define mat:

util.h
Code:
#ifndef TEST
#define TEST

const float mat; // declare mat, not define it: no assignment here

#endif
You can then define mat, just once, in util.c:
Code:
#include "util.h"

const float mat = 100.8/45.0; // defined only once, right here.
iSee is online now   0 Reply With Quote
Old Jul 12, 2007, 06:43 AM   #7
guest42
Thread Starter
macrumors newbie
 
Join Date: Jul 2007
I see what you are saying lazydog, about it 'mostly' protecting it, until it's sitting in two different implemented files. I suppose that's just one of the limitations of the C compiler. But now that I know this, it's not a big deal. I will probably continue to use constants where I can, as opposed to defines, I suppose that for now, what works, works (though I probably should not count on this).
guest42 is offline   0 Reply With Quote
Old Jul 12, 2007, 07:03 AM   #8
lazydog
macrumors 6502a
 
Join Date: Sep 2005
Location: Cramlington, UK
Send a message via MSN to lazydog
guest42, you don't sound entirely confident with using const in this situation! Perhaps it feels a bit dodgy because of you experience of trying to understand why it worked before and not now. I did manage to replicate the problem somewhat by switching between debug and release build mode. In debug mode you example code worked fine, but when I switched to release mode I got the multiple definition error. I'm not sure why it would be the case that the code compiles in debug mode but I suspect it has something to do with zero link and all that clever stuff.


and thanks iSee for actually pointing out the solution.

b e n
lazydog is offline   0 Reply With Quote
Old Jul 12, 2007, 07:09 AM   #9
Nutter
macrumors 6502
 
Join Date: Mar 2005
Location: London, England
Yes, that is Zerolink, may it rest in peace...
Nutter is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Multiple Images when Button pressed..?? Xcode Phloxx1 iPhone/iPad Programming 2 Feb 22, 2014 04:04 PM
iPhone: Battery symbol overlaping the switch camera symbol. becast Jailbreaks and iOS Hacks 4 Feb 7, 2014 07:46 PM
xCode and multiple windows DennisBlah Mac Programming 17 Dec 10, 2013 12:29 AM
Grapher: How to revise definitions ritachengl Mac Applications and Mac App Store 1 Jun 26, 2013 06:44 PM
In need of help with a few beginner definitions! Woodshark69 Mac Programming 13 Dec 27, 2012 02:09 PM

Forum Jump

All times are GMT -5. The time now is 07:53 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC