xcode 声明 extern NSString 导致链接器错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12186631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Declaring extern NSString causes linker error
提问by JSA986
This is ridiculous, im trying to create a sound bool to turn of in app sounds. I keep getting
这太荒谬了,我试图创建一个声音布尔来关闭应用程序中的声音。我不断得到
Undefined symbols for architecture i386:
"_kPlaySoundPrefsKey", referenced from:
-[AppDelegate application:didFinishLaunchingWithOptions:] in AppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have checked that all my files are linked in build phases, I have deleted the appdelegate .m where im getting the error before I even get to call the bool in any of my view controllers, and re imported it in build phases. Checked I have relevant framweworks in place. I have even checked a previous app I made with same code and the code it appears to be exactly the same with no error (built with previous version of xcode). Taking it right back to basics I get the error as soon as I add the following code to my App Delegate,
我已经检查过我的所有文件是否在构建阶段链接,我已经删除了 appdelegate .m 在我什至在我的任何视图控制器中调用 bool 之前我收到错误的地方,并在构建阶段重新导入它。检查我有相关的框架。我什至检查了我用相同代码制作的以前的应用程序,它的代码似乎完全相同,没有错误(使用以前版本的 xcode 构建)。回到基础,将以下代码添加到我的 App Delegate 后,我立即收到错误消息,
.h
。H
#import <UIKit/UIKit.h>
extern NSString *kPlaySoundPrefsKey;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
.m
.m
#import "AppDelegate.h"
#import <AudioToolbox/AudioToolbox.h>
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
NSDictionary *defaultDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:kPlaySoundPrefsKey];
return YES;
}
If I change extern NSString *kPlaySoundPrefsKey;
to NSString *kPlaySoundPrefsKey;
it builds then crashes...Im out of ideas now
如果我改变extern NSString *kPlaySoundPrefsKey;
到 NSString *kPlaySoundPrefsKey;
它建立然后崩溃...林的想法,现在
采纳答案by JSA986
Thanks for your help guys
谢谢你们的帮助
I added
我加了
NSString *kPlaySoundPrefsKey = @"playSoundKey";
NSString *kPlaySoundPrefsKey = @"playSoundKey";
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultDict];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultDict];
to app delegate. That fixed it
应用程序委托。那修好了
回答by idz
When you declaring something as extern
you are telling the compiler the type AND that you will define it somewhere else. In your case you never define your variable.
当您extern
在告诉编译器类型并且您将在其他地方定义它时声明某些内容时。在您的情况下,您永远不会定义您的变量。
So in your .h you would have:
所以在你的 .h 你会有:
extern NSString* kPlaySoundPrefsKey;
But in some .m you must also have
但是在某些 .m 中,您还必须具有
NSString* kPlaySoundPrefsKey = @"play_sounds"; // or some value
Since in your case these are constants you can also specify:
由于在您的情况下这些是常量,您还可以指定:
extern NSString* const kPlaySoundPrefsKey;
and
和
NSString* const kPlaySoundPrefsKey = @"play_sounds";
The addition of the const qualifier will cause a compiler error if someone ever writes something like
如果有人写过类似的东西,添加 const 限定符将导致编译器错误
kPlaySoundPrefsKey = @"some_other_value";
回答by Raj
I am also got the same error even if I am properly declared and define the extern const string , The problem is, my constant file not in the compile source list .
即使我正确声明并定义了 extern const string,我也遇到了同样的错误,问题是,我的常量文件不在编译源列表中。
Make sure that your .m file appears in the "Compile Sources" Build Phase for your build target. Sometimes, adding files to a project in Xcode doesn't add all implementation files to the appropriate targets.
确保您的 .m 文件出现在您的构建目标的“编译源”构建阶段。有时,在 Xcode 中向项目添加文件不会将所有实现文件添加到适当的目标。
Hope this will helpful for some people. Refe: linker-error-undefined-symbols-symbols-not-found
希望这对某些人有帮助。参考:链接器错误未定义符号符号未找到
回答by justin
First, ensure it is defined:
首先,确保它被定义:
// AppDelegate.h
extern NSString* const kPlaySoundPrefsKey; // << declaration
// AppDelegate.m
NSString * const kPlaySoundPrefsKey = @"kPlaySoundPrefsKey"; // << definition
see also:
也可以看看:
"extern const" vs "extern" only
3 questions about extern used in an Objective-C project