On 7/10/19 2:24 PM, G G wrote:
> is it possible that c/c++ programs can start with something other
> than a function named main()?
Since you're explicitly asking about both C and C++, I'm cross-posting
this to comp.lang.c.
Yes it is possible, but only for freestanding implementations:
"In a freestanding environment (in which C program execution may take
place without any benefit of an operating system), the name and type of
the function called at program startup are implementation-defined." (C
standard, 5.1.2p1)
"It is implementation-defined whether a program in a freestanding
environment is required to define a main function." (C++ standard, 6.6.1p1).
> or is it somehow set in the compiler the name of the function
> where to start?
An implementation of C or C++ can conform either as a freestanding
implementation or a hosted one. A hosted implementation must recognize
main() as the starting point. A freestanding implementation may have
other ways of identifying the starting point, but is required to
document them, and (implicitly) is required to conform to whatever
documentation it provides. Either way, it is indeed up to the
implementation to recognize the permitted ways of identifying the
starting point of a program.
It is common, but not essential, for an implementation to be divided
into several separate parts, such as a pre-processor, a compiler, and a
linker. However, the standard says nothing about how the work of an
implementation should be sub-divided. It is only the implementation as a
whole that has responsibility for implementing the correct start point
for a program.
That being said, in a typical pre-processor/compiler/linker setup, it is
indeed the compiler that is required to recognize the start point, but
it's the linker that actually does what's needed to make the program
start there.