Reputation: 40
Can a program be written without main()
function?
I have written this code and saved a filename as withoutmain.c
and getting an error as
undefined reference to 'WinMain@16'"
My code
#include<stdio.h>
#include<windows.h>
extern void _exit(register int code);
_start(){
int retval;
retval=myFunc();
_exit(retval);
}
int myFunc(void){
printf("Hiii Pratishtha");
return 0;
}
Please provide me the solution of this problem and also the proper memory construction of code and what is happening at the compiler end of this program. Thank you!
Upvotes: 1
Views: 3673
Reputation: 1901
Can a program be written without main() function?
Yes there can be a C program without a main function. I would suggest two solutions.......
1) Using a macro that defines main
#include<stdio.h>
#include<windows.h>
#define _start main
extern void _exit(register int code);
int myFunc(void){
printf("Hiii Pratishtha");
return 0;
}
int _start(){
int retval;
retval=myFunc();
_exit(retval);
}
2) Using Entry Point (Assuming you are using visual studio)
To set this linker option in the Visual Studio development environment
/ENTRY:function
A function that specifies a user-defined starting address for an .exe file or DLL.
OR
if you are using gcc
then
-Wl,-e_start
the -Wl
,... thing passes arguments to the linker, and the linker takes a -e
argument to set the entry function
Upvotes: 1