Question on main

A

Angus

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
 
V

Victor Bazarov

Angus said:
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
 
J

James Kanze

(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.

[...]
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top