Program without main

Discussion in 'C Programming' started by Guruz, May 14, 2004.

  1. Guruz

    Guruz Guest

    hi C gurus do anyone of u know how to write a program in C without
    main and still create a executable out of it. Remember, I said no
    main() function not in
    -->include files
    -->libraries
    -->no define thisthat main

    I mean absolutely no main() atall

    Well to start off here one method :
    main() is called in C run time object crt0.o. But if we want to write
    crt0.S of our own what r the things we have to do? can anyone explain
    me whats going on behind this C run time?
     
    Guruz, May 14, 2004
    #1
    1. Advertising

  2. On Fri, 14 May 2004, Guruz wrote:

    > hi C gurus do anyone of u know how to write a program in C without
    > main and still create a executable out of it. Remember, I said no
    > main() function not in
    > -->include files
    > -->libraries
    > -->no define thisthat main
    >
    > I mean absolutely no main() atall
    >
    > Well to start off here one method :
    > main() is called in C run time object crt0.o. But if we want to write
    > crt0.S of our own what r the things we have to do? can anyone explain
    > me whats going on behind this C run time?


    There is no crt0.o on my system. The fact that your compiler calls an
    entry point in crt0.o that in turn calls main is incredibly compiler
    specific.

    It is not uncommon to have some entry point that initializes global
    variables, sets up the stack, etc. and then calls the main function. It is
    often possible to write your own start up routine that does not call main.

    BUT it is not guaranteed that there exists code before main. All the setup
    and initialization could occur by the operating system's loader and your
    program execution actually starts at main. In that case you MUST have a
    main.

    Therefore, whatever solution someone comes up with it will off-topic for
    comp.lang.c. It will only apply to a specific implementation of a C
    compiler.

    --
    Send e-mail to: darrell at cs dot toronto dot edu
    Don't send e-mail to
     
    Darrell Grainger, May 14, 2004
    #2
    1. Advertising

  3. Guruz wrote:
    > hi C gurus do anyone of u know how to write a program in C without
    > main and still create a executable out of it. Remember, I said no
    > main() function not in
    > -->include files
    > -->libraries
    > -->no define thisthat main
    >
    > I mean absolutely no main() atall


    You should invest some time in learning to spell the English
    language correctly. "u" is a letter. "atall" should be "at all".

    Yes, many people know how to write a program in C without a main()
    function. I've done it myself on many embedded systems. On my
    current project, we don't define "main" so that we can use our
    own libraries and start-up code.


    > Well to start off here one method :
    > main() is called in C run time object crt0.o.


    Perhaps with your compiler, but not with mine.


    > But if we want to write
    > crt0.S of our own what r the things we have to do?


    Depends greatly on "ur" implementation (compiler). Some compilers
    allow you to bypass their run-time library and start up code and
    provide "u" w/ the documentation on how "2" do it.


    > can anyone explain
    > me whats going on behind this C run time?


    Yes, many people can.
    In a few words:
    There is a bit of code that is executed before the "main" function.
    This code initializes the Run-Time library and variables.
    Once this is set up the main() function (or "ur" function) can
    then be executed. From this point, execution follows your program.
    Some implementations also have code which is executed upon the
    end or termination of "ur" program.

    For more information, research these topics:
    Compiler Design & Theory
    Operating Systems
    "ur" implementation (compiler, linker, etc.)

    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, May 14, 2004
    #3
  4. Guruz

    Dan Pop Guest

    In <> (Guruz) writes:

    >hi C gurus do anyone of u know how to write a program in C without
    >main and still create a executable out of it. Remember, I said no
    >main() function not in
    >-->include files
    >-->libraries
    >-->no define thisthat main
    >
    >I mean absolutely no main() atall


    Such a program would not be a hosted C program *by definition*.

    >Well to start off here one method :
    >main() is called in C run time object crt0.o. But if we want to write
    >crt0.S of our own what r the things we have to do? can anyone explain
    >me whats going on behind this C run time?


    Nope, because the C language definition is completely silent on this
    issue. The purpose of crt0.o is to interface the code generated by your
    compiler to the OS that is executing it. Therefore, its code is specific
    to both the OS and the C compiler using it. Your only hope is to find
    the source code of your startup module and to study it.

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, May 14, 2004
    #4
  5. Guruz

    Ben Pfaff Guest

    (Guruz) writes:

    > hi C gurus do anyone of u know how to write a program in C without
    > main and still create a executable out of it.


    Perhaps you should look into freestanding implementations.
    --
    "Give me a couple of years and a large research grant,
    and I'll give you a receipt." --Richard Heathfield
     
    Ben Pfaff, May 14, 2004
    #5
  6. Guruz

    Mitchell Guest

    On Fri, 14 May 2004 15:25:05 GMT, Thomas Matthews
    <> wrote:

    >Guruz wrote:
    >> hi C gurus do anyone of u know how to write a program in C without
    >> main and still create a executable out of it. Remember, I said no
    >> main() function not in
    >> -->include files
    >> -->libraries
    >> -->no define thisthat main
    >>
    >> I mean absolutely no main() atall

    >
    >You should invest some time in learning to spell the English
    >language correctly. "u" is a letter. "atall" should be "at all".


    And it should be "does anyone" not "do anyone".

    Yes. In VC++ I see a linker option /ENTRY:function to specify entry
    point function for an executable. However it will bypass calling
    certain C runtime library function, which is needed to perform
    specific initializations. However, this is platform/compiler specific,
    so it's kind of Off Topic.
     
    Mitchell, May 15, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wenjie wang
    Replies:
    5
    Views:
    490
    wenjie wang
    Nov 1, 2004
  2. c program without main( ) ?

    , Jul 15, 2005, in forum: C Programming
    Replies:
    16
    Views:
    1,691
    Chris Hills
    Jul 17, 2005
  3. Spidey
    Replies:
    37
    Views:
    1,289
    Keith Thompson
    Dec 6, 2005
  4. robinsonreyna

    program without main () ?

    robinsonreyna, Jan 9, 2006, in forum: C Programming
    Replies:
    13
    Views:
    630
    Walter Banks
    Jan 10, 2006
  5. knguyen
    Replies:
    9
    Views:
    2,858
    Roedy Green
    Feb 15, 2008
Loading...

Share This Page