Home > Backend Development > C++ > body text

Why Am I Getting the 'Error LNK2019 unresolved external symbol _main' in My C Program?

Mary-Kate Olsen
Release: 2024-11-12 20:21:02
Original
551 people have browsed it

Why Am I Getting the

Error: Unresolved External Symbol _main

In programming, it's common to encounter the error "Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)". This error arises when the compiler cannot find the definition of the main function, which is the entry point of a C program.

Understanding the Error Message

The error message indicates that:

  • The linker is unable to resolve the reference to an external symbol named "_main".
  • This symbol is referenced in a function called "invoke_main".

Cause of the Error

Typically, this error occurs when the main function is not defined or is not properly declared with the appropriate function prototype.

Solution

To resolve this error, ensure the following:

  • Define the main function in your code: In a C program, the main function must be defined as follows:
int main() {
  // Your code here
}
Copy after login
  • Check your project properties: In Visual Studio, verify that the following project property setting is set correctly:

    • Project Properties->Linker->System->SubSystem = Windows

This setting instructs the linker to generate a Windows executable, which requires a main function.

Example Code

For reference, here's a complete example code that includes a working main function:

#include <iostream>

int main() {
  std::cout << "Hello, world!" << std::endl;
  return 0;
}
Copy after login

By following these steps, you should be able to resolve the "Error LNK2019 unresolved external symbol _main" issue and successfully build your C program.

The above is the detailed content of Why Am I Getting the 'Error LNK2019 unresolved external symbol _main' in My C Program?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template