gpt4 book ai didi

iphone - 谁会调用 Objective C 中的 Main 方法

转载 作者:行者123 更新时间:2023-12-03 18:54:35 26 4
gpt4 key购买 nike

我想知道谁会调用objective-c中的main方法?

我知道 UIApplicationMain(nil,nil,nil,NSStringFromClass[Appdelgate class]) 方法正在从 main() 方法调用,然后进程从 appdelegate didFinishLaunchingWithOptions() 方法继续......

我也知道java中的main()方法是由JVM调用的,并且进程是从main()方法进行的。

就像这样,我想知道谁会在 Objective-c 中调用 main() 。

感谢帮助

最佳答案

找出答案的简单方法是在 main() 中设置断点并查看:

call stack

从技术上来说,问题的答案是 main() 是从 start 中调用的。您没有获得 start 的源代码,但如果需要,您可以在调试器中查看汇编代码。该版本来自为模拟器构建的代码:

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

如果您创建一个 MacOS X 命令行程序并在 main() 中放置一个断点,您会发现 main() 调用也可以在桌面上启动。 Mac 版本的 start 程序集并不完全相同,但非常接近。因此,可以猜测 start 是编译器根据目标平台为您生成的,并且 start 是操作系统启动时查找的入口点一个程序。

关于iphone - 谁会调用 Objective C 中的 Main 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11543799/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com