Question on main

Discussion in 'C++' started by Angus, Sep 11, 2009.

  1. Angus

    Angus Guest

    Hello

    I see that if you try to compile without defining a main function that
    you see a link error such as:
    LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main

    Is it possible to NOT link with libd.lib (I presume this is the C
    runtime? And if you did not, would you then need to implement low
    level stuff like how to what exactly?

    I presume function has to be called main() because it is somehow
    defined in C runtime ocde?

    I realise this question is theoretical because there is little
    practical point in not linking with the C runtime - but I am trying to
    understand what is going on under the bonnet.

    Angus
    Angus, Sep 11, 2009
    #1
    1. Advertising

  2. Angus wrote:
    > I see that if you try to compile without defining a main function that
    > you see a link error such as:
    > LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
    >
    > Is it possible to NOT link with libd.lib (I presume this is the C
    > runtime?


    Yes, if you are creating a library, for example. Linking with run-time
    libraries happens when making the resulting executable, and you don't
    actually have to do it. See utility 'ar', for instance.

    All that is OS-specific, however.

    > And if you did not, would you then need to implement low
    > level stuff like how to what exactly?


    No, the library is still linked to the rest of the code, just later,
    when *your* library is used.

    > I presume function has to be called main() because it is somehow
    > defined in C runtime ocde?


    No, it's because the Standard says so. Every program has to have the
    'main' function, that's where the execution of the program begins (well,
    aside from the initialization of static objects).

    > I realise this question is theoretical because there is little
    > practical point in not linking with the C runtime - but I am trying to
    > understand what is going on under the bonnet.


    Get a good book. Try "The C++ Object Model" by Stanley Lippman.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Sep 11, 2009
    #2
    1. Advertising

  3. Angus

    James Kanze Guest

    On Sep 11, 9:12 pm, Victor Bazarov <> wrote:
    > Angus wrote:
    > > I see that if you try to compile without defining a main
    > > function that you see a link error such as:
    > > LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main


    > > Is it possible to NOT link with libd.lib (I presume this is
    > > the C runtime?


    (I think you mean libc.a, under Unix, or one of several
    different runtime libraries under Windows.)

    > Yes, if you are creating a library, for example. Linking with
    > run-time libraries happens when making the resulting
    > executable, and you don't actually have to do it. See utility
    > 'ar', for instance.


    Technically speaking, it depends. The first question concerns
    what level he is talking about. The standard never mentions a
    "C (or C++) runtime", and I never specify that I want to link
    with it. What the compiler (or more technically, the compiler
    driver, e.g. cl or g++) does behind my back is another issue.
    Typically (at least for Unix), the compiler driver will
    automatically link with a number of things you didn't specify:
    the C++ runtime, the C runtime, the system API and a "start-up"
    object file; it is the start-up object file which contains the
    external reference to main. (The exact organization may
    vary---under Unix, for example, most of the C run-time and the
    system API are traditionally in a single library, libc.a, but
    some of the C library is usually in a separate library, libm.a,
    which isn't always linked in automatically; and I think under
    Windows that the C++ runtime, the C runtime and the system API
    are all in the same library.) Under Unix and Windows, however,
    it's also possible to invoke the linker directly, in which case
    the compiler driver doesn't come into play, and nothing is
    implicit. And it's at least theoretically possible to specify
    any entry point you want as the start of the program, and not
    use any of the libraries. (But only theoretically, since the
    libraries also contain the system API, and a program which never
    calls the system isn't likely to do anything useful.)

    > All that is OS-specific, however.


    Very.

    [...]
    > > I presume function has to be called main() because it is
    > > somehow defined in C runtime code?


    > No, it's because the Standard says so. Every program has to
    > have the 'main' function, that's where the execution of the
    > program begins (well, aside from the initialization of static
    > objects).


    That's according to the standard (and only for hosted
    implementations). At the lowest level of the implementation,
    the start-up routine defines what the system sees as the entry
    point (and it won't be called main). And much, if not all, of
    the start-up routine is probably written in C, or even in C++.

    (Of course, none of this should be of any interest to you unless
    you are writing a compiler.)

    --
    James Kanze
    James Kanze, Sep 12, 2009
    #3
    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. John M
    Replies:
    1
    Views:
    1,115
    Kumar Reddi
    May 29, 2005
  2. Hal Styli
    Replies:
    14
    Views:
    1,615
    Old Wolf
    Jan 20, 2004
  3. Frederick Ding

    int main() or int main(void)?

    Frederick Ding, Dec 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    641
  4. Ravi
    Replies:
    17
    Views:
    912
    Kenneth Brody
    Apr 1, 2006
  5. main() called inside main()

    , May 4, 2006, in forum: C Programming
    Replies:
    14
    Views:
    1,409
    Richard Heathfield
    May 7, 2006
Loading...

Share This Page