ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Home » C programs » Common C program Errors

Error: undefined reference to 'main' in C

Here, we will learn why an error: undefined reference to 'main' occurs and how to fix it in C programming language?
Submitted by IncludeHelp, on September 04, 2018

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;
}

Output

Hello world!




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.