How to compile C source code without a main function?

前端 未结 5 436
-上瘾入骨i
-上瘾入骨i 2020-12-13 02:25

How can I compile my C source files without needing to put a main function within them?

I get an error for the .cfiles that have no main fu

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-13 02:53

    Suppose you have hello.c:

    #include
    #include
    _start()
    {
       exit(my_main());
    }
    int my_main()
    {
       printf("Hello");
       return 0;
    }
    

    Compile as:

    gcc  -nostartfiles  hello.c 
    

    and you can get an executable out of it.

提交回复
热议问题