Pratishtha Sharma
Pratishtha Sharma

Reputation: 40

Error Undefined reference to 'WinMain@16'

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

Answers (1)

Mohan
Mohan

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.

  1. Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
  2. LClick the Linker folder.
  3. Click the Advanced property page.
  4. Modify the Entry Point property.

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

Related Questions