Undefined symbols: "_OBJC_CLASS_$ error

38,271

Solution 1

In general, this will occur when the code for BoxView is not being compiled into your target correctly.

identity editor screenshot

You need to ensure that the target you're building has its corresponding box checked for your BoxView.m implementation file. Your question suggests that you've tried this, but here's a screenshot (from Xcode 4) just for clarity's sake.

A 'Clean and Build' never hurts, either.

Solution 2

I just want to add that Ben Mosher's answer is totally right. But there's another way to include the files to build in the Target settings.

enter image description here

Solution 3

Added Scenario

If your project has module dependencies(framework), rebuild them before building your main project.

Share:
38,271
cms
Author by

cms

Updated on September 04, 2020

Comments

  • cms
    cms over 3 years

    I've been looking through countless posts about this error:

    Undefined symbols:
    "_OBJC_CLASS_$_BoxView", referenced from:
      objc-class-ref-to-BoxView in ViewMovingViewController.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    

    BoxView is a subclass of UIView, and the UIKit framework has been included. BoxView.h has been imported in the ViewController.

    The ViewController contains this code:

    -(void) addBoxViewAtLocation:(CGPoint)point {
        CGRect rect;  
        rect.origin.x = point.x;  
        rect.origin.y = point.y;  
        rect.size.width = 80;  
        rect.size.width = 40;  
        BoxView *newView = [[BoxView alloc] initWithFrame:rect];  
        newView.backgroundColor = [UIColor yellowColor];  
        [mainView addSubview:newView];  
    }  
    

    And BoxView contains this code:

    - (id)initWithFrame:(CGRect)frame {     
        self = [super initWithFrame:frame];  
        if (self) {  
            // no further initialization  
        }  
        return self; 
    }  
    

    This is the line that's causing the error, from the code above:

    BoxView *newView = [[BoxView alloc] initWithFrame:rect];
    

    When I change BoxView to UIView in that line, the error goes away. Does anyone know what I need to change here? I have looked through many posts about this, but most answers say it's link related, but I've tried ticking and unticking certain boxes with no success. I'm wondering if the error is in my code? Any suggestions would be appreciated!

  • cms
    cms over 12 years
    Thank you, definitely tried the clean build but haven't come across a tick box for BoxView.m .. Where would I find that tickbox? I'm looking through the Build settings but don't see it.. I'm working with an older version of X-Code, X-Code 3, so I don't think my version conforms to the screenshot you sent
  • cms
    cms over 12 years
    Oh actually, I just found it. Don't know if its the case in Xcode 4 but in Xcode 3 you can literally drag and drop the .m file into the target
  • wcochran
    wcochran about 12 years
    Sometimes when I add files to projects they are not compiled when the project is built. In Xcode 4, I explicitly add them under "Build Phases" -> "Compile Sources". Why these are not set (sometimes) by default, I don't know.
  • Ben Mosher
    Ben Mosher almost 12 years
    @cms If this did solve (or help with) your problem, it would be great if you could accept the answer. :-)
  • Patrice Cote
    Patrice Cote almost 12 years
    I can't tell how you made my day with this answer. Thanks a lot for sharing the knowledge.
  • Sjakelien
    Sjakelien almost 11 years
    Thanks a lot! I don't want to claim that I now understand all the ins and outs of this problem, but hey, I can compile my app!
  • e.ozmen
    e.ozmen over 9 years
    You saved my day!! Thanks
  • ecoe
    ecoe almost 8 years
    works in Xcode 7 very similarly but you access Linked Frameworks and Libraries from the General tab for a given selected target.
  • Julius Naeumann
    Julius Naeumann about 3 years
    I got this error because I defined a class in a header file, without any @implementation. Adding an empty @implementation into the .m solved my issue.