Why am I getting "_OBJC_CLASS_$..., referenced from:" linker error when I have correctly linked frameworks?

29,149

Solution 1

Quick Answer

Copy and paste the following line into your build settings:

GCC_SYMBOLS_PRIVATE_EXTERN = NO

In the target build settings look for "Symbols Hidden by Default". For the Debug configuration you want "No".

I've had this problem on and off for many months and I've just discovered why.

Solution 2

I faced a similar problem. I got the linker error:

_OBJC_CLASS_$_MyClass

The problem was that I had declared an @interface for MyClass but had not declared its corresponding @implementation.

The fix was to simply add

@implementation MyClass
@end

Solution 3

Not sure if this could be the problem, but with the new compiler, any obj-c that aren't explicitly referred to/invoked will not be linked from libraries. This causes problems when you implement categories in libraries for example.

Try adding '-ObjC' to 'additional linker flags' in the build settings panel for your target. shrug

Share:
29,149
John Gallagher
Author by

John Gallagher

Ruby developer. Obsessed with creating simple code and delivering masses of business value. Inverting dependencies one day at a time.

Updated on July 24, 2020

Comments

  • John Gallagher
    John Gallagher almost 4 years

    My Problem

    I getting "_OBJC_CLASS_$..., referenced from:" linker error when compiling some Xcode projects (it happens in both iOS and Mac projects) I have correctly linked frameworks and imports.

    Setup

    • One application target
    • One test target
    • All frameworks linked correctly

    On compile I get the following linker errors: "_OBJC_CLASS_$_JGCountdownTimer", referenced from: objc-class-ref in JGCountdownTimerTestCase.o

    for many classes that are used in tests.

    What I've Tried

    • Checked that imports are all present
    • Removed all non standard frameworks
    • If I compile a class for both the test target and the app target it fixes the issue. But then I get other warnings from the compiler.
  • John Gallagher
    John Gallagher about 12 years
    Thanks for the quick answer - I was posting this because I actually found the solution and it drove me freaking CRAZY that I couldn't find the answer anywhere else so I thought I'd post it here for some other hapless soul who gets this linker error and can't understand why.
  • Lily Ballard
    Lily Ballard about 12 years
    This answer is a bit misleading. When linking static libraries, by default the compiler (gcc and clang) will only link members of the library that are referenced somehow. Specifying the -ObjC linker flag means link all members that have Obj-C class/category information. This doesn't affect dylibs at all, or application code, just static libs.
  • nielsbot
    nielsbot about 12 years
    kevin: true... it was a stab in the dark a bit :)
  • Chris Hillery
    Chris Hillery about 12 years
    You can also prefix your @interface with __attribute__((visibility("default")))
  • Prakash Desai
    Prakash Desai almost 11 years
    In build setting where to paste that GCC thing?
  • Zia
    Zia about 8 years
    Search for GCC_SYMBOLS_PRIVATE_EXTERN and then set it to No
  • NotABot
    NotABot over 7 years
    When i search for GCC_SYMBOLS_PRIVATE_EXTERN I get Symbols Hidden by Default which is already set to No. Any other solution?
  • ceztko
    ceztko about 4 years
    @Wevah Thanks so much for your comment! I would have loved if you posted your solution as an answer. I ending posting my experience with the technique in this answer