How C Compiler identifies main() function in the C Program

This is a discussion on How C Compiler identifies main() function in the C Program within the C Programming forums, part of the General Programming Boards category; How C Compiler identifies main() function in the C Program ???...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    How C Compiler identifies main() function in the C Program

    How C Compiler identifies main() function in the C Program ???

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    The assembly stub code that starts up the program specifically calls main(). The prototype for main () is given to you by your compiler documentation. If you change main() to an unrecognizable signature the program will complain about an unresolved external reference to _main(<blah, blah, blah>).

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In other words, the compiler doesn't "recognize" main [1].

    When you compile a program, the compiler produces an object file. It then links [2] the object file from your source code with a startup runtime component (usually called crt0.o[bj]) and the C library components (such as printf).

    crt0.o or whatever it is called MUST be first. It contains a label called "start" or something similar. That is where the application will start. From start (possibly via other stops on the way), main is called.

    [1] Some compilers DO treat a function called main as "special" - but that's not the rule, and it's for other purposes than "where to start" - gcc puts some extra startup-code in C++ code, to run the global object construction before the main code of your application starts, for example.

    [2] For more complex programs, the process is to compile many source files to object files, and when all have been compiled, the linker is invoked.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  3. Basic program design, passing pointer to a function
    By heras in forum C Programming
    Replies: 14
    Last Post: 04-02-2008, 04:21 AM
  4. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  5. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21