In C++ Main Function Is the Entry Point to Program How to Change It to an Other Function

in c++ main function is the entry point to program how i can change it to an other function?

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?

Change the entry point main

gcc -o entry_test -Wl,-eother entry_test.c

#include<stdio.h>       

int other(void){//can't use argc, argv
printf("Bye-Bye world!\n");
return 0;
}

int main(int argc, char *argv[]){
printf("%s\n","Hello world!");
return 0;
}

How to change entry point of C program with gcc?

It's a linker setting:

-Wl,-eentry

the -Wl,... thing passes arguments to the linker, and the linker takes a -e argument to set the entry function

how to change entry point function of GNU C++ program?

The test() function must be declared with extern "C" to avoid the C++ name mangling:

extern "C" int test(int argc, char *argv[]);

can C _start function be made to call entry point function other than main?

Yes it may be possible, however it’s all up to the compiler if that feature is supported or not. So you will have to look it up in your compiler manual.

How to change the name of executable entry point from main to something else?

As by C Standard, the hosted environment (that I guess is your case as/if you want to use standard library headers*) forces you to keep with main function. From C11 §5.1.2.2.1/p1 (emphasis mine):

The function called at program startup is named main. The
implementation declares no prototype for this function. It shall be
defined
with a return type of int and with no parameters:

int main(void) { /* ... */ }

or with two parameters (referred to here as
argc and argv, though any names may be used, as they are local to the
function in which they are declared):

int main(int argc, char *argv[]) { /* ... */ }

or equivalent;10) or in some other
implementation-defined manner.

There are two options that I can think of to bypass main function requirement in your situation:

  1. Create separate object file (that is, by gcc -c) with main symbol, that just (declares and) calls (i.e. wraps) your custom entry point (possibly passing through argc and argv invocation arguments). This would make linker happy and is as simple as adding single makefile rule.
  2. Compile your program as gcc -DWhatever=main. This essentially replaces every instance of Whatever preprocessing token by main, so linker thinks of Whatever as "proper" main function.

* some headers must be available in freestanding environment too, such as <stddef.h> and <float.h>, see §4/p6 for full list of them.

Here is some basic ilustration of both options. Each assumes, that foo.c is as following:

foo.c

#include <stdio.h>

void foo(void)
{
printf("foo\n");
}

First method:

main.c

/* declare an entry point */
void foo(void);

/* define main as wrapper function */
int main(void)
{
foo();

return 0;
}

Compile & run:

$ gcc -c main.c
$ gcc foo.c main.o
$ ./a.out
foo

Second method:

$ gcc -Dfoo=main foo.c 
$ ./a.out
foo

Some things may require more tweaking, but I hope you catch the idea.



Related Topics



Leave a reply



Submit