c program without main( ) ?

Discussion in 'C Programming' started by leeaby@gmail.com, Jul 15, 2005.

  1. Guest

    Hi, I will appreciate your assistance.

    Can we write a c code which do not contain main()
    I have heard that this is possible. Is it really possible?

    Thanks for your help in advance

    lee
    , Jul 15, 2005
    #1
    1. Advertising

  2. Sensei Guest

    On 2005-07-15 13:15:15 -0500, said:

    > Hi, I will appreciate your assistance.
    >
    > Can we write a c code which do not contain main()
    > I have heard that this is possible. Is it really possible?



    Yes, a library would do that.

    --
    Sensei <>

    cd /pub
    more beer
    Sensei, Jul 15, 2005
    #2
    1. Advertising

  3. David Lago Guest

    wrote:
    > Hi, I will appreciate your assistance.
    >
    > Can we write a c code which do not contain main()
    > I have heard that this is possible. Is it really possible?


    Yes, it is, but you can only obtain object code out of it, instead of an
    executable file...

    David.
    --
    David Lago <>

    PGP key available at: http://pgp.mit.edu
    Personal Blizog: http://arcanelinux.org/blog/dlworld
    The Enflame Project: http://enflame.org
    David Lago, Jul 15, 2005
    #3
  4. wrote:

    > Hi, I will appreciate your assistance.
    >
    > Can we write a c code which do not contain main()
    > I have heard that this is possible. Is it really possible?


    Yes, of course. Win32 programmers do it all the time.

    C implementations which specify an entry point other than main are generally
    called "freestanding" implementations, and requirements on them are far
    less severe than on hosted implementations, this laxity over the name of
    the entry point being one example of the lower requirements.

    (In Win32 programs, the entry point is typically called WinMain.)

    A more common situation in which we write C code without main() is when
    we're writing a code library, rather than an actual program. The library is
    built separately, without main(), and then linked to whichever programs
    wish to use it.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    mail: rjh at above domain
    Richard Heathfield, Jul 15, 2005
    #4
  5. wrote on 15/07/05 :
    > Hi, I will appreciate your assistance.
    >
    > Can we write a c code which do not contain main()
    > I have heard that this is possible. Is it really possible?


    It's not possible to write a conforming C program without a main(). But
    the main() function could be embedded in some 'framework library' (Like
    a GUI Windows application for example).

    I do that when I write code for dev-C++ :

    I have a 'framework.c' containing the main() function :

    (missing code at http://mapage.noos.fr/emdel/clib.htm)

    #include "ed/inc/sysalloc.h"
    #include <string.h>
    int main_ (int argc, char *argv[]);

    static int is_opt (char const *const sopt, int const argc, char *const
    *const argv)
    {
    int ok = 0;
    int i;

    for (i = 1; i < argc; i++)
    {
    if (strcmp (sopt, argv) == 0)
    {
    ok = 1;
    break;
    }
    }
    return ok;
    }

    static void onexit (void)
    {
    sys_mem_trace ();
    system ("pause");
    }

    int main (int argc, char *argv[])
    {
    static char Trace[1 << 11];
    int trace = TRACE_OFF;

    atexit (onexit);

    if (argc > 1)
    {
    if (is_opt ("/t", argc, argv))
    {
    trace = TRACE_ON;
    }
    }

    sys_mem_init (Trace, sizeof Trace, trace, NULL);

    main_ (argc, argv);

    return 0;
    }


    and in my 'main.c', I have :

    #define main main_
    #if 0
    #include "ed/inc/sysalloc.h"
    #include "ed/inc/sys.h"
    #undef assert
    #define assert(e) ASSERT (e)
    #else
    #include <assert.h>
    #endif
    /*
    ----------------------------------------------------------------------
    */

    int main ()
    {

    /* device under test */

    return 0;
    }


    BTW, why do you ask ? What exactly is your goal ?

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
    Emmanuel Delahaye, Jul 15, 2005
    #5
  6. Mark Guest

    "Richard Heathfield" <> wrote in message
    news:db8vc6$48$-infra.bt.com...
    > wrote:
    >> Hi, I will appreciate your assistance.
    >> Can we write a c code which do not contain main()
    >> I have heard that this is possible. Is it really possible?

    > Yes, of course. Win32 programmers do it all the time.
    > C implementations which specify an entry point other than main are
    > generally
    > called "freestanding" implementations, and requirements on them are far
    > less severe than on hosted implementations, this laxity over the name of
    > the entry point being one example of the lower requirements.
    > (In Win32 programs, the entry point is typically called WinMain.)
    > A more common situation in which we write C code without main() is when
    > we're writing a code library, rather than an actual program. The library
    > is
    > built separately, without main(), and then linked to whichever programs
    > wish to use it.


    I've encountered situations in which main() was defined by a library which
    provided an external function for the application to hook into. That
    sucked!
    Fortunately that code now resides in India!

    Mark
    Mark, Jul 15, 2005
    #6
  7. Mark Guest

    "Emmanuel Delahaye" <> wrote in message
    news:...
    > wrote on 15/07/05 :
    >> Hi, I will appreciate your assistance.
    >>
    >> Can we write a c code which do not contain main()
    >> I have heard that this is possible. Is it really possible?

    >
    > It's not possible to write a conforming C program without a main(). But
    > the main() function could be embedded in some 'framework library' (Like a
    > GUI Windows application for example).
    >
    > I do that when I write code for dev-C++ :

    <snip garbage>

    It would be cleaner if you were to provide an initialization function which
    could be called on startup, why didn't you? What happens when the code
    you want to test needs to link with another shitty library which also
    decided
    to define main() ?

    Just curious,
    Mark
    Mark, Jul 15, 2005
    #7
  8. Mark wrote on 15/07/05 :
    >> I do that when I write code for dev-C++ :

    > <snip garbage>


    Garbage ? Thanks...

    > It would be cleaner if you were to provide an initialization function which
    > could be called on startup, why didn't you? What happens when the code
    > you want to test needs to link with another shitty library which also decided
    > to define main() ?
    >
    > Just curious,


    Linker error ?

    This code is designed to test code (like the one posted by people I try
    to help on the forums.

    It happens that some of this code can be strange (to me), like having
    several return from main() or a lot of exit()'s spread out. This is the
    way I found to handle that. If you have a better solution, I'd be glad
    to see it.

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Mal nommer les choses c'est ajouter du malheur au
    monde." -- Albert Camus.
    Emmanuel Delahaye, Jul 15, 2005
    #8
  9. Mark Guest

    "Emmanuel Delahaye" <> wrote in message
    news:...
    > Mark wrote on 15/07/05 :
    >>> I do that when I write code for dev-C++ :

    >> <snip garbage>

    >
    > Garbage ? Thanks...

    OK, a little harsh, sorry.

    >> It would be cleaner if you were to provide an initialization function
    >> which
    >> could be called on startup, why didn't you? What happens when the code
    >> you want to test needs to link with another shitty library which also
    >> decided
    >> to define main() ?
    >>
    >> Just curious,

    >
    > Linker error ?
    >
    > This code is designed to test code (like the one posted by people I try to
    > help on the forums.
    >
    > It happens that some of this code can be strange (to me), like having
    > several return from main() or a lot of exit()'s spread out. This is the
    > way I found to handle that. If you have a better solution, I'd be glad to
    > see it.


    I've already given you the suggestion, here it is again:
    >> It would be cleaner if you were to provide an initialization function
    >> which
    >> could be called on startup


    The solution would be to rename your 'main()' function
    calling it something intuitive, such as init_testlib();

    Then when you want to help someone, you can still copy and paste their code
    and need only to add 1 line to the main() function (a call to
    init_testlib(argc, argv);)

    As for multiple return statements and calls to exit... it makes no
    difference to your
    library as you have registered yourself with the atexit() function... you
    don't perform
    ANY calculations or do a cleanup of any sort when main returns control to
    you!
    Should you have such a requirement in the future, I'd suggest creating
    another
    function to perform the post-run tasks.

    Regards,
    Mark
    Mark, Jul 15, 2005
    #9
  10. S.Tobias Guest

    Richard Heathfield <> wrote:
    > wrote:
    >
    >> Hi, I will appreciate your assistance.
    >>
    >> Can we write a c code which do not contain main()
    >> I have heard that this is possible. Is it really possible?

    >
    > Yes, of course. Win32 programmers do it all the time.
    >
    > C implementations which specify an entry point other than main are generally
    > called "freestanding" implementations, and requirements on them are far
    > less severe than on hosted implementations, this laxity over the name of
    > the entry point being one example of the lower requirements.
    >
    > (In Win32 programs, the entry point is typically called WinMain.)


    So you mean to say that Windows+MSVC is a freestanding implementation?

    --
    Stan Tobias
    mailx `echo LID | sed s/[[:upper:]]//g`
    S.Tobias, Jul 15, 2005
    #10
  11. S.Tobias wrote:

    > Richard Heathfield <> wrote:
    >>
    >> C implementations which specify an entry point other than main are
    >> generally called "freestanding" implementations, and requirements on them
    >> are far less severe than on hosted implementations, this laxity over the
    >> name of the entry point being one example of the lower requirements.
    >>
    >> (In Win32 programs, the entry point is typically called WinMain.)

    >
    > So you mean to say that Windows+MSVC is a freestanding implementation?


    Depends how you invoke the compiler. If you tell it "console app", then no,
    it's hosted all right. But yes, if you say to MSVC "this one's a GUI
    program", it's basically a freestanding imp. Which explains a lot. :)

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    mail: rjh at above domain
    Richard Heathfield, Jul 15, 2005
    #11
  12. Artie Gold Guest

    Richard Heathfield wrote:
    > S.Tobias wrote:
    >
    >> Richard Heathfield <> wrote:
    >>> C implementations which specify an entry point other than main are
    >>> generally called "freestanding" implementations, and requirements on them
    >>> are far less severe than on hosted implementations, this laxity over the
    >>> name of the entry point being one example of the lower requirements.
    >>>
    >>> (In Win32 programs, the entry point is typically called WinMain.)

    >> So you mean to say that Windows+MSVC is a freestanding implementation?

    >
    > Depends how you invoke the compiler. If you tell it "console app", then no,
    > it's hosted all right. But yes, if you say to MSVC "this one's a GUI
    > program", it's basically a freestanding imp. Which explains a lot. :)
    >

    Freefalling?
    --ag
    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
    Artie Gold, Jul 16, 2005
    #12
  13. Nigel Horne Guest

    On Fri, 15 Jul 2005 11:15:15 -0700, leeaby wrote:

    > Hi, I will appreciate your assistance.
    >
    > Can we write a c code which do not contain main()
    > I have heard that this is possible. Is it really possible?


    Yes - just write your own replacement to crtStart

    > Thanks for your help in advance
    Nigel Horne, Jul 16, 2005
    #13
  14. In article <>,
    Nigel Horne <> wrote:
    >On Fri, 15 Jul 2005 11:15:15 -0700, leeaby wrote:


    >> Can we write a c code which do not contain main()
    >> I have heard that this is possible. Is it really possible?


    >Yes - just write your own replacement to crtStart


    crtStart is far from portable. I don't even recognize the implementation.
    Nothing I've ever worked with.
    --
    Any sufficiently advanced bug is indistinguishable from a feature.
    -- Rich Kulawiec
    Walter Roberson, Jul 16, 2005
    #14
  15. Malcolm Guest

    "Nigel Horne" <> wrote
    >> Can we write a c code which do not contain main()
    >> I have heard that this is possible. Is it really possible?

    >
    > Yes - just write your own replacement to crtStart
    >

    Almost never will a hosted implementation initiate a program by calling
    main() directly. usually there is some housekeeping / startup code called
    first. Sometimes it is possible to override this with the C compiler,
    sometimes not.
    The name of the actual startup module may well be crtStart on your system,
    but this isn't a standard or even a conventional name (on old DOS compilers
    it used to be _main).
    Malcolm, Jul 16, 2005
    #15
  16. Chris Hills Guest

    In article <>,
    writes
    >Hi, I will appreciate your assistance.
    >
    >Can we write a c code which do not contain main()
    >I have heard that this is possible. Is it really possible?
    >
    >Thanks for your help in advance
    >
    >lee
    >


    In freestanding program (no OS, usually embedded systems) there is no
    requirement for main in the standard.

    However as all freestanding programs require some start up assembler
    before the C to set up memory spaces, stack etc this code usually ends
    with a jump to label "main" by convention not requirement.

    You are free to change this to any label you like and some do. However
    some debugging tools, notably ICE, often require that there is a main
    for their debugging.



    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris Hills, Jul 17, 2005
    #16
  17. Chris Hills Guest

    In article <dbbuv7$6k9$-infra.bt.com>, Malcolm
    <> writes
    >
    >"Nigel Horne" <> wrote
    >>> Can we write a c code which do not contain main()
    >>> I have heard that this is possible. Is it really possible?

    >>
    >> Yes - just write your own replacement to crtStart
    >>

    >Almost never will a hosted implementation initiate a program by calling
    >main() directly. usually there is some housekeeping / startup code called
    >first. Sometimes it is possible to override this with the C compiler,
    >sometimes not.


    Don't you mean freestanding?


    --
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
    \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
    /\/\/ www.phaedsys.org \/\/\
    \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
    Chris Hills, Jul 17, 2005
    #17
    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:
    479
    wenjie wang
    Nov 1, 2004
  2. Guruz

    Program without main

    Guruz, May 14, 2004, in forum: C Programming
    Replies:
    5
    Views:
    6,061
    Mitchell
    May 15, 2004
  3. Spidey
    Replies:
    37
    Views:
    1,270
    Keith Thompson
    Dec 6, 2005
  4. robinsonreyna

    program without main () ?

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

Share This Page