5

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

Mahesh Peri
  • 1,689
  • 2
  • 14
  • 24

4 Answers4

3

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.

Caleb
  • 124,013
  • 19
  • 183
  • 272
2

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

Ivor Prebeg
  • 988
  • 6
  • 11
2

Since Objective-C is a derivative of C, much of the internals are similar (which is why you can mix C\C++ code in with Objective-C)). As such, when both compile into executables, they work off of similar systems. This is also why Objective-C is compiled by gcc or Clang.

With that in mind, what happens when you compile? All of your C code gets converted into Assembly\machine code (since Assembly is just a mnemonic version of machine code). Assembly is on a lower level than Java; the CPU just runs through it and executes each individual instruction sequentially. The main() function in C languages is where the compiler 'knows' where the instructions start at. After you do all of your declarations, the main() function gets jumped to with an instruction called 'jmp'.

So what happens when you compile in Xcode and launch? First, gcc/Clang converts the code into Assembly\machine code. Second, Xcode loads the binary file into memory. Third, Darwin (OS X or iOS, or any OS for that matter) recognizes that this is an executable area of memory, and starts from the top of the instruction set and runs through each instruction.

To answer your question, the main() function is started by the CPU. This is different than Java, which has a platform-independent instruction set that is 'emulated' in the JVM.

This question has a great answer which explains how the main() function gets translated into Assembly code.

Community
  • 1
  • 1
Kyle Lacy
  • 2,278
  • 1
  • 21
  • 29
2

In most systems, the OS has special functions responsible for loading executable programs into memory. They allocate the required memory and load the information from the file into that space. In some cases they may even make special modifications to the code so that it works in the specific memory space where it's being loaded.

After loading the program into memory, the OS passes control to the beginning of the program file's code segment. The beginning of the code segment usually has a small routine which does a few necessary operations like initialize the stack and memory heap. When those things are done, it then passes off control to the main() function.

Mike Fulton
  • 920
  • 11
  • 22