Newbie Objective C Error such as _main - objective-c

I've been tinkering all day, and I can't seem to fix this error.
Here's the code:
//
// main.m
// Learning ObjC
//
// Created by Nickirv on 8/9/15.
// Copyright (c) 2015 Nickirv. All rights reserved.
//
#import <Foundation/Foundation.h>
#interface Person: NSObject{
int age;
int weight;
}
-(void) print;
-(void) setAge: (int) a;
-(void) SetWeight: (int) w;
#end
And it outputs this issue:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I would appreciate any help! Thank you very much!

You can't delete the main boilerplate code:
int main(int argc, const char * argv[]) {
#autoreleasepool {
}
return 0;
}
In the end Objective-C is "C" and the program starts execution by calling main. Additionally Objective-C code needs to execute in an autoreleasepool.
You define class #interfaces and #implemtations outside of (generally above) the boilerplate but the first line of code to run must be within the autoreleasepool scope {}.
Here is an example Objective-C program similar to what the OP seems to want using #properties for simplicity and demonstration.
It is important to study Objective-C documentation until the following code is fully understood, td;dr does not work for this.
#import <Foundation/Foundation.h>
#interface Person: NSObject
#property int age;
#property int weight;
- (void)print;
#end
#implementation Person : NSObject
- (void)print {
printf("Age: %i, weight: %i", self.age, self.weight);
}
#end
int main(int argc, const char * argv[]) {
#autoreleasepool {
Person *don = [[Person alloc] init];
don.weight = 130;
don.age = 23;
[don print];
}
return 0;
}
Output:
Age: 23, weight: 130

Related

Property issue in Objective C, 'score' not found on object of type pointer

I just started out programming in Objective C. This is very simple stuff but im wondering what im missing on my knowledge of properties, as far as I understand this should work but it gives me the error:
Property 'score' not found on object of type 'Player *'
I have a Player class and the code as follows below, each bolded is a separate file
Player.h has:
#import <Foundation/Foundation.h>
#interface Player : NSObject
#property int score;
#end
Player.m has:
#import "Player.h"
#implementation Player
- (id)init {
self = [super init];
if (self){
_score = 5000;
}
return self;
}
#end
main.m has
#import <Foundation/Foundation.h>
#import "Player.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
Player *firstPlayer = [[Player alloc] init];
NSLog(#"The default score is %i", [firstPlayer score]);
}
return 0;
}
Player.h is missing the following line which should go between the #import and #property lines:
#interface Player : NSObject
Where NSObject may be a different class, but needs to be whatever you intend to be subclassing.

Objective-C: Use of undeclared identifier

I know I'm probably making a stupid mistake but I'm working my way through the book Programming in Objective-C and I'm getting a couple of errors but I cant seem to find the mistake.
person.m
#import "person.h"
#implementation person{
int age;
}
-(void) print{
NSLog(#"the person is %i years old", age);
}
-(void) setAge:(int)a{
age = a;
}
#end
person.h
#import <Foundation/Foundation.h>
#interface person : NSObject
-(void) print;
-(void) setAge: (int)a;
#end
main
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
#autoreleasepool {
person *newPerson; //error is on this line use of undeclared identifier:person
}
return 0;
}
You need to import person.h header in all files you want to use Person class, so add
#import "person.h"
line to your main file

Why clang doesn't find .h file in #include "" in the same directory?

This is my first question on this site, so please be patient with me. I tried searching for an answer but couldn't find any relevant.
I have main.m, Person.h and Person.h files in a current directory. In main.m I include Person.h. Then I try to compile main.m, but it gives an error that Person object was not found.
Here is main.m:
#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[])
{
#autoreleasepool {
// Create an instance of Person
Person *person = [[Person alloc] init];
[person setWeightInKilos:96];
[person setHeightInMeters:1.8];
float bmi = [person bodyMassIndex];
NSLog(#"person has a BMI of %f", bmi);
}
return 0;
}
Person.h:
#import <Foundation/Foundation.h>
#interface Person : NSObject
{
// 2 instance variables
float heightInMeters;
int weightInKilos;
}
// instance methods
- (void)setHeightInMeters:(float)h;
- (void)setWeightInKilos:(float)w;
- (float)bodyMassIndex;
#end
Person.m:
#import <Foundation/Foundation.h>
#implementation Person
- (void)setHeightInMeters:(float)h
{
heightInMeters = h;
}
- (void)setWeightInKilos:(float)w
{
weightInKilos = w;
}
- (float)bodyMassIndex
{
return weightInKilos / (heightInMeters * heightInMeters);
}
#end
Here is the error I get as I try to compile using 'cc main.m -framework Foundation':
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_Person", referenced from:
objc-class-ref in main-24c686.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I even tried adding the current directory in PATH, but it didn't help.
Thanks in advance for help.
That error is not about the include file "Person.h". The problem is that the Person
class it not found by the linker.
The reason is that you did not add the "Person.m" file to your command line:
cc main.m Person.m -framework Foundation

How to run a simple objective-c in linux

EDITED
I have been trying to start coding in Objective-c. Its just a simple program to try getter and setter methods. Also print Hello World. THe following is my code:
#import <objc/Object.h>
#interface Car:Object{
int wheel: 5;
}
- (int)wheel;
- (void)setWheel: (int)newWheel;
#end
#include <stdio.h>
#implementation Car
- (int)wheel{
return wheel;
}
- (void)setWheel: (int)newWheel{
wheel = newWheel;
}
#end
#include <stdlib.h>
int main(void){
printf("Hello World");
}
I now get garbage
/tmp/cc3UC6jY.o: In function `__objc_gnu_init':
hello.m:(.text+0x6d): undefined reference to `__objc_exec_class'
/tmp/cc3UC6jY.o:(.data+0x1c0): undefined reference to `__objc_class_name_Object'
collect2: error: ld returned 1 exit status
I used the the command gcc -o hello hello.m -lobjc
I have spent hours googling this answer.
The following variation of your code compiled and ran for me:
#import <objc/Object.h>
#interface Car : Object {
int wheel;
}
- (int)wheel;
- (void)setWheel: (int)newWheel;
#end
#implementation Car
- init {
wheel = 5;
return self;
}
- (int)wheel {
return wheel;
}
- (void)setWheel: (int) newWheel {
wheel = newWheel;
}
#end
#include <stdio.h>
int main(void){
printf("Hello World\n");
id myCar = [[Car alloc] init];
printf("Wheel value is %d\n", [myCar wheel]);
return 0;
}

Objective-c: Now allowing me to create an instance of a class

I just started programming in objective-c(today) and I'm just doing some simple command line examples but I've already run into something that has stumped me. I am getting a compiler error on lines 5 & 6 in main. Here is my code:
Fraction Interface:
#import <Foundation/Foundation.h>
#interface Fraction : NSObject
{
int numerator, denominator;
}
-(void)print;
-(void)setDenominator;
-(void)setNumerator;
#end
Fraction implementation:
#import "Fraction.h"
#implementation Fraction
-(void)print {
NSLog(#"%i/%i",numerator,denominator);
}
-(void)setNumerator: (int) n {
numerator = n;
}
-(void)setDenominator: (int) d {
denominator = d;
}
#end
Main:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
#autoreleasepool {
Fraction *f1 = [[Fraction alloc] init];
Fraction *f2 = [[Fraction alloc] init];
}
return 0;
}
All 3 files are in the same folder. I'm using Xcode; if this is relevant information.
Looks like you forgot to add #import "Fraction.h" to the top of main.m, In the future adding the error you receive would be helpful.