Is a main() required for a C program?

后端 未结 7 433
后悔当初
后悔当初 2020-12-15 03:13

Well the title says it all. Is a main() function absolutely essential for a C program?

I am asking this because I was looking at the Linux kernel code,

相关标签:
7条回答
  • 2020-12-15 03:38

    In machine language things get executed sequentially, what comes first is executed first. So, the default is for the compiler place a call to you main method to fit the C standard.

    Your program works like a library, which is a collection of compiled functions. The main difference between a library and a standard executable is that for the second one the compiler generates assembly code which calls one of the functions in your program.

    But you could write assembly code which calls your an arbitrary C program function (the same way calls to library functions work, actually) and this would work the same way other executables do. But the thing is you cannot do it in plain standard C, you have to resort to assembly or even some other compiler specific tricks.

    This was intended as a general and superficial explanation, there are some technical differences I avoided on purpose as they don't seem relevant.

    0 讨论(0)
  • 2020-12-15 03:39

    main is called by glibc,that is a part of application(ring 3), not the kernel(ring 0).
    the driver has another entry point,for example windows driver base on WDM is start from DRIVERENTRY

    0 讨论(0)
  • 2020-12-15 03:40

    Well, no, but ...

    C99 specifies that main() is called in the hosted environment "at program startup", however, you don't have to use the C runtime support. Your operating system executes image files and starts a program at an address provided by the linker.

    If you are willing to write your program to conform to the operating system's requirements rather than C99's, you can do it without main(). The more modern (and complex) the system, though, the more trouble you will have with the C library making assumptions that the standard runtime startup is used.

    Here is an example for Linux...

    $ cat > nomain.S
    .text
    _start:
        call    iamnotmain
        movl    $0xfc, %eax
        xorl    %ebx, %ebx
        int     $0x80
    .globl _start
    $ cat > demo.c
    
    void iamnotmain(void) {
        static char s[] = "hello, world\n";
        write(1, s, sizeof s);
    }
    $ as -o nomain.o nomain.S
    $ cc -c demo.c
    $ ld -static nomain.o demo.o -lc
    $ ./a.out
    hello, world
    

    It's arguably not "a C99 program" now, though, just a "Linux program" with a object module written in C.

    0 讨论(0)
  • 2020-12-15 03:53

    The main() function is called by an object file included with the libc. Since the kernel doesn't link against the libc it has its own entry point, written in assembler.

    0 讨论(0)
  • 2020-12-15 03:57

    Paxdiablo's answer covers two of the cases where you won't encounter a main. Let me add a couple of more:

    • Many plug-in systems for other programs (like, say, browsers or text editors or the like) have no main().
    • Windows programs written in C have no main(). (They have a WinMain() instead.)
    0 讨论(0)
  • 2020-12-15 03:58

    No, the ISO C standard states that a main function is only required for a hosted environment (such as one with an underlying OS).

    For a freestanding environment like an embedded system (or an operating system itself), it's implementation defined. From C99 5.1.2:

    Two execution environments are defined: freestanding and hosted. In both cases, program startup occurs when a designated C function is called by the execution environment.

    In a freestanding environment (in which C program execution may take place without any benefit of an operating system), the name and type of the function called at program startup are implementation-defined.

    As to how Linux itself starts, the start point for the Linux kernel is start_kernel though, for a more complete picture of the entire boot process, you should start here.

    0 讨论(0)
提交回复
热议问题