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.
我在头文件const双EARTH_RADIUS=6353中声明了一个常量;它被导入到不同的头文件中,我得到了一个链接器错误。
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?
基本上,我希望常数对我项目中的所有类都可用。我应该在哪里申报?
4 个解决方案
#1
88
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)
然后在某处的。m文件中(通常是。m代表你声明的。h)
const double EARTH_RADIUS = 6353;
#2
62
There are two ways to accomplish this:
有两种方法可以做到这一点:
1st option- As previous replies pointed, in the .h file:
第一个选项-正如之前的回复所指出的,在.h文件中:
myfile.h
extern const int MY_CONSTANT_VARIABLE;
and in myfile.m define them
myfile。m定义它们
myfile.m
const int MY_CONSTANT_VARIABLE = 5;
2nd option- My favourite:
第二选项-我最喜欢的:
myfile.h
static const int MY_CONSTANT_VARIABLE = 5 ;
#3
4
Declare it in a source file and have external linkage to it( using extern keyword ) so as to use in all other source files.
在源文件中声明它,并与它有外部链接(使用extern关键字),以便在所有其他源文件中使用。
#4
1
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.
最佳实践是在.h和.m文件中声明它。在Objective-C中看到关于这个相同问题的非常详细的答案集。
#1
88
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)
然后在某处的。m文件中(通常是。m代表你声明的。h)
const double EARTH_RADIUS = 6353;
#2
62
There are two ways to accomplish this:
有两种方法可以做到这一点:
1st option- As previous replies pointed, in the .h file:
第一个选项-正如之前的回复所指出的,在.h文件中:
myfile.h
extern const int MY_CONSTANT_VARIABLE;
and in myfile.m define them
myfile。m定义它们
myfile.m
const int MY_CONSTANT_VARIABLE = 5;
2nd option- My favourite:
第二选项-我最喜欢的:
myfile.h
static const int MY_CONSTANT_VARIABLE = 5 ;
#3
4
Declare it in a source file and have external linkage to it( using extern keyword ) so as to use in all other source files.
在源文件中声明它,并与它有外部链接(使用extern关键字),以便在所有其他源文件中使用。
#4
1
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.
最佳实践是在.h和.m文件中声明它。在Objective-C中看到关于这个相同问题的非常详细的答案集。