Reputation: 33138
I declared a constant in a header file const double EARTH_RADIUS=6353;
that is imported to various other headers and I got a linker error.
Ld /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator/BadgerNew.app/BadgerNew normal i386
cd /Users/Teguh/Dropbox/badgers/BadgerNew
setenv MACOSX_DEPLOYMENT_TARGET 10.6
setenv PATH "/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/llvm-g++-4.2 -arch i386 -isysroot /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk -L/Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator -F/Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator -filelist /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/BadgerNew.LinkFileList -mmacosx-version-min=10.6 -Xlinker -objc_abi_version -Xlinker 2 -framework CoreLocation -framework UIKit -framework Foundation -framework CoreGraphics -framework CoreData -o /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Products/Debug-iphonesimulator/BadgerNew.app/BadgerNew
ld: duplicate symbol _EARTH_RADIUS in /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/NearbyIsiKota.o and /Users/Teguh/Library/Developer/Xcode/DerivedData/BadgerNew-bjopcgcgsjkcvcevflfbvsjwfgnu/Build/Intermediates/BadgerNew.build/Debug-iphonesimulator/BadgerNew.build/Objects-normal/i386/FrontPageofBadger.o for architecture i386
collect2: ld returned 1 exit status
Basically I want the constant to be available for all classes on my project. Where should I declare it?
Upvotes: 56
Views: 74385
Reputation: 39480
Best practice would be to declare it in your .h and .m files. See Constants in Objective-C for a very detailed set of answers regarding this same question.
Upvotes: 1
Reputation: 3875
There are two ways to accomplish this:
1st option- As previous replies pointed, in the .h file:
myfile.h
extern const int MY_CONSTANT_VARIABLE;
and in myfile.m define them
myfile.m
const int MY_CONSTANT_VARIABLE = 5;
2nd option- My favourite:
myfile.h
static const int MY_CONSTANT_VARIABLE = 5 ;
Upvotes: 65
Reputation: 34665
Declare it in a source file and have external linkage to it( using extern keyword ) so as to use in all other source files.
Upvotes: 4
Reputation: 17732
You can declare in the header, define it in a code file. Simply declare it as
extern const double EARTH_RADIUS;
then in a .m file somewhere (usually the .m for the .h you declared it in)
const double EARTH_RADIUS = 6353;
Upvotes: 98