views:

311

answers:

6

i have been asked in a interview question to change the entry point of a c or c++ program from main() to any other function how is it possible?

thanx in advance.

+4  A: 

If you are on VS2010, this could give you some idea

As it is easy to understand, this is not mandated by the C++ standard and falls in the domain of 'implemenation specific behavior'.

Chubsdad
+14  A: 

In standard C (and, I believe, C++ as well), you can't, at least not for a hosted environment (but see below). The standard specifies that the starting point for the C code is main. The standard (c99) doesn't leave much scope for argument:

5.1.2.2.1 Program startup: (1) The function called at program startup is named main.

That's it. It then waffles on a bit about parameters and return values but there's really no leeway there for changing the name.

That's for a hosted environment. The standard also allows for a freestanding environment (i.e., no OS, for things like embedded systems). For a freestanding environment:

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. Any library facilities available to a freestanding program, other than the minimal set required by clause 4, are implementation-defined.

You can use "trickery" in C implementations so that you can make it look like main isn't the entry point. This is in fact what early Windows compliers did to mark WinMain as the start point.


First way: a linker may include some pre-main startup code in a file like start.o and it is this piece of code which runs to set up the C environment then call main. There's nothing to stop you replacing that with something that calls bob instead.


Second way: some linkers provide that very option with a command-line switch so that you can change it without recompiling the startup code.


Third way: you can link with this piece of code:

int main (int c, char *v[]) { return bob (c, v); }

and then your entry point for your code is seemingly bob rather than main.


However, all this, while of possibly academic interest, doesn't change the fact that I can't think of one single solitary situation in my many decades of cutting code, where this would be either necessary or desirable.

I would be asking the interviewer: why would you want to do this?

paxdiablo
as tagged it is a question in my interview i don't now is it possible or not if possible then how thanx for ans.
moon
+1 for making `main` just call something else. That's the most straightforward and cross-platform method to accomplish this.
Seth
Isn't it possible to do some implementation defined hackery with the constructors of global objects? Demons flying out your nose on other platforms guaranteed, of course.
slartibartfast
+1  A: 

Modify the crt object that actually calls the main() function, or provide your own (don't forget to disable linking of the normal one).

Ignacio Vazquez-Abrams
+3  A: 

From C++ standard docs 3.6.1 Main Function,

A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function.

So, it does depend on your compiler/linker...

liaK
That's interesting. There's no indication in cpp0x that the first sentence there applies only to hosted, and `shall` is normally a standards word that means it _must_ be so. So it appears that `main` must exist even for freestanding. Can someone with more C++-Standard-fu than me figure that out? Can you _have_ a `main` without being required to define it??
paxdiablo
Actually that appears to be the case for C as well. I was looking at the hosted section. The freestanding section is a bit looser with the requirements. +1.
paxdiablo
@paxdiablo: I think the intent is that the second sentence qualifies the first sentence, so that it means "A program [in a hosted environment] shall contain..." I think "shall contain" means "shall contain [a definition of];" I don't know how else the word "contain" could be understood.
James McNellis
@paxdiablo, I did refer from N2461 draft one.. Haven't updated to the recent though.. Am I making a mistake over here??
liaK
No, I don't think you're making a mistake here, liaK, there's similar language in C99.
paxdiablo
@James, I think you're probably right in that it applies only to hosted (again because C99 makes a big distinction as well). I don't know what environment ISO were thinking of when they relaxed the rules that much. Even in the _tiniest_ embedded systems I worked on, there was still startup code and a `main` :-)
paxdiablo
Alf P. Steinbach
+1  A: 

On windows there is another (rather unorthodox) way to change the entry point of a program: TLS. See this for more explanations: http://isc.sans.edu/diary.html?storyid=6655

ruslik
+1  A: 

For Solaris Based Systems i have found this, You can use the .init section for every platforms i guess:

   pragma init (function [, function]...)
        This pragma causes each listed function to be called during initialization (before main) or during shared module loading, by adding a call to the .init section. 
Green Code