How do you build a C program that includes the entry point on Mac OS X?

懵懂的女人 提交于 2019-12-12 01:18:34

问题


How do you build a C program that includes the entry point on Mac OS X?

I want to build:

start() {
    /* exit system call */
    asm("movl $1,%eax;"
        "xorl %ebx,%ebx;"
        "int  $0x80"
    );
}

but when I run:

gcc -nostdlib min.c

I always get:

ld: could not find entry point "start" (perhaps missing crt1.o)
collect2: ld returned 1 exit status

The one other attempt I made just to see what it was doing was run:

gcc -nostdlib -c min.c && otool -tV min.o

And the output was:

(__TEXT,__text) section
_start:
0000000000000000    pushq   %rbp
0000000000000001    movq    %rsp,%rbp
0000000000000004    leave
0000000000000005    ret

So where did that underscore come from before the "start" function? How do I prevent that from happening? Or more simply:

How do you build a C program that includes the entry point on Mac OS X?

Thanks, CrazyChenz


回答1:


The gcc -e option defines the entry point, when you want the entry point to be something other than start. This way you can create mystart() as you entry point.

gcc -e mystart mycode.c -o mycode

I do not know how to set the -e option in Xcode.



来源:https://stackoverflow.com/questions/4353928/how-do-you-build-a-c-program-that-includes-the-entry-point-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!