The error: undefined reference to 'main' in C program is a very stupid mistake by the programmer, it occurs when the main() function does not exist in the program. If you used main() function and still the error is there, you must check the spelling of the main() function.
Consider the given example, here I wrote mian() instead of main(), see the spelling of main() which is not correct in the program.
Example:
#include <stdio.h>
int mian(void) {
printf("Hello world!");
return 0;
}
Output
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function '_start':
(.text+0x20): undefined reference to 'main'
collect2: error: ld returned 1 exit status
How to fix?
To fix this error, correct the spelling of the main() function.
Correct code:
#include <stdio.h>
int main(void) {
printf("Hello world!");
return 0;
}
Consider the given example, here I wrote mian() instead of main(), see the spelling of main() which is not correct in the program.
Example:
Output
How to fix?
To fix this error, correct the spelling of the main() function.
Correct code:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer