Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who will call Main method in objective c

Tags:

iphone

I want to know who will call main method in objective-c?

I know the UIApplicationMain(nil,nil,nil,NSStringFromClass[Appdelgate class]) method is calling from main() method and then process continues from appdelegate didFinishLaunchingWithOptions() method.....

And i also know that main() method in java is called by JVM and process is going on from main() method.

Like that, i want to know who will call main() in objective-c.

thanks for help

like image 366
Mahesh Peri Avatar asked Jul 18 '12 14:07

Mahesh Peri


People also ask

What is main in Objective-C?

main is the name of the function that is called when a program first starts.


2 Answers

The easy way to find out is to stick a breakpoint in main() and take a look:

call stack

So technically, the answer to the question is that main() is called from something called start. You don't get the source code for start, but you can look at the assembly code in the debugger if you want. This version is from code built for the simulator:

0x1c30:  pushl  $0
0x1c32:  movl   %esp, %ebp
0x1c34:  andl   $-16, %esp
0x1c37:  subl   $16, %esp
0x1c3a:  movl   4(%ebp), %ebx
0x1c3d:  movl   %ebx, (%esp)
0x1c40:  leal   8(%ebp), %ecx
0x1c43:  movl   %ecx, 4(%esp)
0x1c47:  addl   $1, %ebx
0x1c4a:  shll   $2, %ebx
0x1c4d:  addl   %ecx, %ebx
0x1c4f:  movl   %ebx, 8(%esp)
0x1c53:  movl   (%ebx), %eax
0x1c55:  addl   $4, %ebx
0x1c58:  testl  %eax, %eax
0x1c5a:  jne    0x00001c53               ; start + 35
0x1c5c:  movl   %ebx, 12(%esp)
0x1c60:  calll  0x00001c70               ; main at main.m:9
0x1c65:  movl   %eax, (%esp)
0x1c68:  calll  0x00002376               ; exit
0x1c6d:  hlt    
0x1c6e:  nop    
0x1c6f:  nop   

If you create a MacOS X command-line program and put a breakpoint in main(), you'll find that main() is called by start on the desktop, too. The assembly for the Mac version of start isn't exactly the same, but it's very close. So it's a good guess that start is generated for you by the compiler based on the target platform, and that start is the entry point that the operating system looks for when it's launching a program.

like image 121
Caleb Avatar answered Oct 24 '22 05:10

Caleb


I believe it is called by assembly routine from Obj-C runtime library, same as in C but with C runtime library

check this: https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html

like image 35
Ivor Prebeg Avatar answered Oct 24 '22 05:10

Ivor Prebeg