How to make another function entry point other than main () in C
Each program has main()
, and the execution of the program begins with it. Is it possible to write a program without main()
and make another function an entry point? If so, can someone please tell me how this can be done? Am I using Linux?
If you are compiling with gcc, specifying the option -e <symbol>
will allow you to change the entry point to a function symbol()
.
There is a solution for building an executable shared library where you can build a program using another function as an entry point.
The code looks like this:
#include <stdio.h>
#include <stdlib.h>
const char __invoke_dynamic_linker[] __attribute__ ((section (".interp")))
= "/lib/ld-linux.so.2";
void fun()
{
printf("This is fun./n");
exit(0);
}
Then build your program as a shared library and specify func as the entry point:
$ gcc -fpic -shared -o fun.so -Wl,-e,fun fun.c $ ./fun.so
The problem with this method is that func cannot have normal arguments like we have in the main function, this is because we don't have a c library to initialize the main arguments.