Error: Id returned 1 exit status (undefined reference to 'main')

Posted in /   /  

Error: Id returned 1 exit status (undefined reference to 'main')
vinaykhatri

Vinay Khatri
Last updated on November 21, 2024

    "Error: Id returned 1 exit status (an 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.

    In this article, we will talk about the Id returned 1 exit status error, the cause behind its occurrence, and a simple way to fix it. So, let's get started!

    What is the main() function?

    • Every C++ and C program contains a mandatory inbuilt function called main().
    • The compiler starts the execution from the main function.
    • The compiler automatically invokes the main function and tries to compile all the code line by line from top to bottom.

    Linker Error

    Linker error generally occurs when we link different invalid object files with the main object file. In this error, the compiler is unable to load the executable file because of the wrong prototyping, and incorrect header files.

    Error Cause

    Here are the two key causes for the error:

    • The user commits some mistakes 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')

    How to 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-mentioned error examples by writing the code appropriately as shown below:

    #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 use the correct letter case while writing the program. The error "undefined reference to main" occurs when we mistype or misspell the main() function.

    People are also reading:

    Leave a Comment on this Post

    0 Comments