Home > Backend Development > C++ > Why am I getting the 'Error LNK2019 unresolved external symbol _main' in my C application?

Why am I getting the 'Error LNK2019 unresolved external symbol _main' in my C application?

Susan Sarandon
Release: 2024-11-22 09:00:12
Original
332 people have browsed it

Why am I getting the

Error: Resolving External Symbols in C Applications

Problem:

When building a C application, you may encounter the following error:

Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@YAHXZ)
Copy after login

Cause:

This error typically arises when the following conditions are not met:

  • The project's entry point function main() is not defined or is not accessible from the main module.
  • The linker cannot locate the definition of main() in the specified library.

Solution:

To resolve this issue, follow these steps:

1. Ensure the Existence of main() Function:

Verify that your project includes a function called main() in the source code. This function serves as the entry point for the program and must be defined as follows:

int main(int argc, char** argv)
{
    // Your program logic goes here
    return 0;
}
Copy after login

2. Linker Settings:

Check your project's linker settings to ensure that the main() function is accessible from the main module. In Visual Studio, go to:

  • Project Properties
  • Linker
  • System
  • Under Subsystem, select Windows

3. Verify Library Inclusion:

Confirm that the library containing the definition of main() is included in the project's linker settings. This can be found in:

  • Project Properties
  • Linker
  • Input
  • Additional Dependencies

Example Project:

#include "windows.h"

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
    MessageBox(NULL, _T("Hello, world!"), _T("My Application"), MB_OK);

    return 0;
}
Copy after login

Note: In this example, main() is declared using the WINAPI macro because the WinMain() function is the entry point for Windows applications.

The above is the detailed content of Why am I getting the 'Error LNK2019 unresolved external symbol _main' in my C application?. 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