Declaring extern NSString causes linker error

后端 未结 4 1657
旧巷少年郎
旧巷少年郎 2021-02-07 11:17

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\",         


        
相关标签:
4条回答
  • 2021-02-07 12:11

    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

    Linker error using extern "C" in Objective-C code

    0 讨论(0)
  • 2021-02-07 12:15

    Thanks for your help guys

    I added

    NSString *kPlaySoundPrefsKey = @"playSoundKey";

    [[NSUserDefaults standardUserDefaults] registerDefaults:defaultDict];

    to app delegate. That fixed it

    0 讨论(0)
  • 2021-02-07 12:17

    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 .

    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.

    Hope this will helpful for some people. Refe: linker-error-undefined-symbols-symbols-not-found

    0 讨论(0)
  • 2021-02-07 12:18

    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.

    So in your .h you would have:

    extern NSString* kPlaySoundPrefsKey;
    

    But in some .m you must also have

    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

    kPlaySoundPrefsKey = @"some_other_value";
    
    0 讨论(0)
提交回复
热议问题