Object c undefined symbol compilation error

Help me please! my first lens program c. The textbook was followed word for word, but it gives me this error that I do not know how to read for an objective c.

SimpleCar.h:

#import <Cocoa/Cocoa.h> @interface SimpleCar : NSObject { NSString* make; NSString* model; NSNumber* vin; } // set methods - (void) setVin: (NSNumber*)newVin; - (void) setMake: (NSString*)newMake; - (void) setModel: (NSString*)newModel; // convenience method - (void) setMake: (NSString*)newMake andModel: (NSString*)newModel; // get methods - (NSString*) make; - (NSString*) model; - (NSNumber*) vin; @end 

SimpleCar.m:

 #import "SimpleCar.h" @implementation SimpleCar // set methods - (void) setVin: (NSNumber*)newVin { [vin release]; vin = [[NSNumber alloc] init]; vin = newVin; } - (void) setMake: (NSString*)newMake { [make release]; make = [[NSString alloc] initWithString:newMake]; } - (void) setModel: (NSString*)newModel { [model release]; model = [[NSString alloc] initWithString:newModel]; } // convenience method - (void) setMake: (NSString*)newMake andModel: (NSString*)newModel { // Reuse our methods from earlier [self setMake:newMake]; [self setModel:newModel]; } - (NSString*) make { return make; } - (NSString*) model { return model; } - (NSNumber*) vin { return vin; } -(void) dealloc { [vin release]; [make release]; [model release]; [super dealloc]; } @end 

CarApp.m:

 #import <Foundation/Foundation.h> #import "SimpleCar.h" int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; SimpleCar *myCar = [[SimpleCar alloc] init]; NSNumber *newVin = [NSNumber numberWithInt:123]; [myCar setVin:newVin]; [myCar setMake:@"Honda" andModel:@"Civic"]; NSLog(@"The car is: %@ %@", [myCar make], [myCar model]); NSLog(@"The vin is: %@", [myCar vin]); [myCar release]; [pool drain]; return 0; } 

compiler call:

 bash-3.2$ gcc CarApp.m SimpleCar.m -g -m64 

Mistake:

 Undefined symbols for architecture x86_64: "___CFConstantStringClassReference", referenced from: CFString in ccR0Zlgm.o CFString in ccR0Zlgm.o CFString in ccR0Zlgm.o CFString in ccR0Zlgm.o "_objc_msgSend", referenced from: _main in ccR0Zlgm.o -[SimpleCar setVin:] in ccJfVliU.o -[SimpleCar setMake:] in ccJfVliU.o -[SimpleCar setModel:] in ccJfVliU.o -[SimpleCar setMake:andModel:] in ccJfVliU.o (maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc ) "_NSLog", referenced from: _main in ccR0Zlgm.o "_objc_msgSend_fixup", referenced from: l_objc_msgSend_fixup_alloc in ccR0Zlgm.o l_objc_msgSend_fixup_release in ccR0Zlgm.o l_objc_msgSend_fixup_release in ccJfVliU.o l_objc_msgSend_fixup_alloc in ccJfVliU.o (maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc ) "_OBJC_CLASS_$_NSAutoreleasePool", referenced from: objc-class-ref in ccR0Zlgm.o "_OBJC_CLASS_$_NSNumber", referenced from: objc-class-ref in ccR0Zlgm.o objc-class-ref in ccJfVliU.o "_objc_msgSendSuper2", referenced from: -[SimpleCar dealloc] in ccJfVliU.o "_OBJC_METACLASS_$_NSObject", referenced from: _OBJC_METACLASS_$_SimpleCar in ccJfVliU.o "__objc_empty_cache", referenced from: _OBJC_METACLASS_$_SimpleCar in ccJfVliU.o _OBJC_CLASS_$_SimpleCar in ccJfVliU.o "__objc_empty_vtable", referenced from: _OBJC_METACLASS_$_SimpleCar in ccJfVliU.o _OBJC_CLASS_$_SimpleCar in ccJfVliU.o "_OBJC_CLASS_$_NSObject", referenced from: _OBJC_CLASS_$_SimpleCar in ccJfVliU.o "_OBJC_CLASS_$_NSString", referenced from: objc-class-ref in ccJfVliU.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status 
+2
objective-c compiler-errors
Jun 03 2018-11-18T00:
source share
2 answers

It looks like you just created an Xcode workspace without an Xcode project. Here is a project you could use:

http://www.markdouma.com/developer/CarApp.zip

Typically, you simply select File> New Project to create a new project. You will probably need a Foundation-based command-line tool for this particular project.

Unfortunately, the guide did not have a better implementation of the SimpleCar class. The following is one possible rewrite:

 #import "SimpleCar.h" @implementation SimpleCar -(void) dealloc { [vin release]; [make release]; [model release]; [super dealloc]; } // set methods - (void) setVin: (NSNumber*)newVin { [newVin retain]; [vin release]; vin = newVin; // [vin release]; // vin = [[NSNumber alloc] init]; // vin = newVin; } 

The commented code above is the source code. This is both potentially dangerous and a memory leak. The first line of source code releases vin before doing anything else, which is potentially dangerous. For example, what if in your CarApp.m you did this:

 NSNumber *vinValue = [car vin]; [car setVin:vin]; NSLog(@"car vin == %@", [car vin]); // unpredictable results/crash 

The source code would release the existing vin value without bothering to ensure that the value passed was not actually vin . By setting vin = newVin , he set vin to indicate to himself, but after it was released. Any subsequent attempts to send messages to vin will lead to failure or unpredictable results.

Source code also leaks memory. NSNumber instances NSNumber immutable, so creating a number via alloc/init does not make much sense (since it will always be zero and can never be changed). My replacement code first saves the value that is passed if it is vin . Then it releases vin and then assigns vin newVin .

The setMake: and setModel: are problematic for the same reasons.

 - (void) setMake: (NSString*)newMake { [newMake retain]; [make release]; make = newMake; // [make release]; // make = [[NSString alloc] initWithString:newMake]; } - (void) setModel: (NSString*)newModel { [newModel retain]; [model release]; model = newModel; // [model release]; // model = [[NSString alloc] initWithString:newModel]; } // convenience method - (void) setMake: (NSString*)newMake andModel: (NSString*)newModel { // Reuse our methods from earlier [self setMake:newMake]; [self setModel:newModel]; } - (NSString*) make { return make; } - (NSString*) model { return model; } - (NSNumber*) vin { return vin; } @end 
+1
Jun 04 2018-11-11T00:
source share
β€” -

At the command line:

 bash-3.2$ gcc CarApp.m SimpleCar.m -g -m64 

You do not include the necessary frameworks for Cocoa and others.

My suggestion is to build your application using Xcode, not the command line.

If you want and try from the command line, add all the frameworks you use with the -framework linker -framework , for example:

 -framework Cocoa 

but then you will also need another switch defining the SDK that you create against (I suppose), for example:

 -isysroot /Developer/SDKs/MacOSX10.6.sdk 

EDIT: if you want to use Xcode 4 to create your project, follow these steps:

  • open Xcode and create a new project: File> New> New Project;

  • select the type of project that suits you (suitable command line tool);

  • add SimpleCar.c / h and CarApp.c to your project; delete the old main.c file;

    (4. If you choose a different project type Command Line Tool instead of adding CarApp.c to the project, copy the contents of your main () function to the -awakeFromNib function found in ApplicationDelegate.)

+6
Jun 03 '11 at 18:57
source share



All Articles