
“Error: Id returned 1 exit status (undefined reference to ‘main’)” is a very common C and C++ linker Error.
Just by looking at the statement, we can tell that there is an error in the main function.
main() function
- Every C++ and C program contain a mandatory inbuilt function called
main().
- The compiler starts the execution from the main function.
- The compiler automatically invokes the main function and try to compile all the code line by line from top to bottom.
Linker Error
Linker error generally occurs when we link the different invalid object file with the main object file. In this error, the compiler could not be able to load the executable file because of the wrong prototyping, and incorrect header files.
Error Cause
- This error occurs typically when the user commit some mistake while writing the main function.
- The
main()
function is not written in lower case.
Error example:
Example 1
#include <stdio.h> int Main() // main() is not written in lowercase { printf("Welcome to TechGeekBuzz"); return 0; }
Output:
[Error]: Id returned 1 exit status (undefined reference to ‘main’)
Example 2
#include <stdio.h> int kain() // Mistype main() with kain() { printf("Welcome to TechGeekBuzz"); return 0; }
Output
[Error]: Id returned 1 exit status (undefined reference to 'main')
Fix the Error
- To fix the error check how you have written the
main()
function. - The
main()
function must be written in lowercase with correct spelling.
Example
Let’s fix the above error example:
#include <stdio.h> int main() { printf("Welcome to TechGeekBuzz"); return 0; }
Output
Welcome to TechGeekBuzz
Conclusion
C and C++ are case-sensitive languages, so it is necessary to write the correct case while writing the program. The error “undefined reference to main” occurs when we miss type or miss spell the main() function.