Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-07-2011, 05:37 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 7
KeitZer is on a distinguished road
Default Duplicate symbol with File Scope (?) variables

I have a few variables in a .h called PListData.h

Code:
BOOL musicOn, soundOn;

@interface PListData : NSObject
{
}
//methods for saving to / getting from plist
Now you'll notice they are above the @interface, aka, can be accessed from any file. I have it set up so I can just call them directly knowing they have the same value. These variables get set and checked in various files throughout my project (when it saves the new value for whether sound/music is on, or when it checks the value before playing a sound file).

HOWEVER, when I decided we needed box2D in our project, I changed one of my implementation files from ' .m ' to ' .mm ' (enables Objective-C and C++ in the same file, or so they say). When I tried running to my device to make sure this change went smoothly, I received a linker error:

Code:
ld: duplicate symbol _musicOn in /Users/KeitZer/Library/Developer/Xcode/DerivedData/iGolfMini-afhuszztmyjgvxcjskpqmdgcquou/Build/Intermediates/iGolfMini.build/Debug-iphoneos/iGolfMini.build/Objects-normal/armv6/HoleDisplayLayer.o and /Users/KeitZer/Library/Developer/Xcode/DerivedData/iGolfMini-afhuszztmyjgvxcjskpqmdgcquou/Build/Intermediates/iGolfMini.build/Debug-iphoneos/iGolfMini.build/Objects-normal/armv6/AppDelegate.o for architecture armv6
collect2: ld returned 1 exit status
Command /old Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-g++-4.2 failed with exit code 1
I am aware this means it's being seen in both HoleDisplayLayer.mm and AppDelegate.mm but doesn't this mean that musicOn is being declared twice? I only set musicOn in the PListData.m when I get the information from th plist, and get musicOn in files that have music playing.

Anyways, the question I had was regarding how to fix it. Maybe I did it wrong, but when I tried using them as instance variables with getters and setters, it wouldn't retain the value (isn't there retain with @property?, but that's Objects-only, right?) through multiple classes. Like, let say I call the set method in the Menu class, but when I call the get in the Display, it returns a different value. Now, I was using [[PListData alloc] setMusic] format, which iirc doesn't save since isn't that a new instance of PListData?


How can I go about using variables through multiple classes?

Any questions left unaswered, feel free to ask and I'll do my best to answer.


Thanks,
KeitZer
KeitZer is offline   Reply With Quote
Old 09-07-2011, 05:59 AM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Quote:
(isn't there retain with @property?, but that's Objects-only, right?)
Yes, only objects can be retained. For simple data types you can use 'assign'.

If you have some data that needs to be accessed by several classes, like a global source, you could create a new singleton class and put it there. Then just get the shared instance from whatever class you want and read or write the data.
baja_yu is offline   Reply With Quote
Old 09-07-2011, 06:16 AM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 7
KeitZer is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
Yes, only objects can be retained. For simple data types you can use 'assign'.

If you have some data that needs to be accessed by several classes, like a global source, you could create a new singleton class and put it there. Then just get the shared instance from whatever class you want and read or write the data.
Wow. Never knew about these (been developing for barely 6 months for iOS, and about 2 years of schooling before that) and ya, that is probably the best solution.
KeitZer is offline   Reply With Quote
Old 09-07-2011, 06:39 AM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

And, it's super easy to implement, this is practically all you need: Loading…
baja_yu is offline   Reply With Quote
Old 09-07-2011, 07:28 AM   #5 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 7
KeitZer is on a distinguished road
Default

Quote:
Originally Posted by baja_yu View Post
And, it's super easy to implement, this is practically all you need: Loading…
thanks!
KeitZer is offline   Reply With Quote
Old 09-07-2011, 11:32 AM   #6 (permalink)
Registered Member
 
Join Date: Jun 2009
Location: Ypsilanti, Michigan
Age: 63
Posts: 1,549
RLScott is on a distinguished road
Default

I don't think the duplicate symbol error had anything to do with going to a .mm file instead if a .m file. I think the error came from the fact that your .h file is serving as a definition as well as a declaration of the variables. The .h file should never define (i.e. Reserve memory) variables. It should only declare them. In the case of global variables like you were using, that means using "extern" in the definition. If you happen to include your .h file into two different implementation files, then both implementation files are trying to define the variable, hence the duplicate symbol error.

The simple solution is to put "extern" in front of the declarations in the .h file. That makes them declarations only. Then define the variables in just one .m or .mm file (before the @implementation so that it is global). For more info see this tutorial.
RLScott is offline   Reply With Quote
Reply

Bookmarks

Tags
duplicate symbol, linker, variable scope

Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 379
8 members and 371 guests
apatsufas, JackReidy, jeroenkeij, Sami Gh, tim0504, UMAD, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,671
Threads: 94,121
Posts: 402,904
Top Poster: BrianSlick (7,990)
Welcome to our newest member, JackReidy
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:53 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0