// Windows Programming Tutorial Series
#include "stdafx.h"
#include <windows.h>
int WINAPI
WinMain(HINSTANCE hInst, 
     HINSTANCE hPrevInstance,
 LPSTR lpCmdLine,
        int nCmdShow)
{
 MessageBox (NULL, "Hello World! This is my first WIN32 program", 
                "Lesson 1", MB_OK);
 return 0;
}

2 errors:
Linking...
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/a.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
a.exe - 2 error(s), 0 warning(s)

Who can make some suggestions please?
I guess it may be I did not include the lib well.

Dani AI

Generated

The linker error means the C runtime was looking for a console entry point (main) while your source defines a GUI entry point (WinMain). There are three practical ways to fix it: build the project as a Windows (GUI) program so the runtime looks for WinMain; keep it as a console project and add a main(); or explicitly tell the linker which entry/startup to use. was pointing at the first approach; that is the usual, clean fix.

For Visual Studio, change the project so the linker uses the Windows subsystem (this makes the CRT call the WinMain startup). If you prefer the command-line or want to be explicit, pass the linker flags to select the subsystem and startup routine:

/SUBSYSTEM:WINDOWS
/ENTRY:WinMainCRTStartup

If the project uses Unicode, the wide-entrypoint variant (wWinMain) is what the CRT will call — or use the generic _tWinMain to be portable.

When your project is configured for Unicode, match the signature accordingly. Examples (signatures only):

int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpCmdLine, int nCmdShow)
int WINAPI _tWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR lpCmdLine, int nCmdShow)

Troubleshooting notes: defining a preprocessor macro like _WINDOWS does not change the linker subsystem — it only affects compilation. Also, the “missing semicolon” suggestion from isn’t applicable here; was correct to point that out. If after changing subsystem you still get unresolved symbols, check that you haven’t set “Ignore All Default Libraries,” that the Entry Point isn’t overridden to something incorrect, and that you’re building the same Configuration (Debug/Release) whose settings you edited. Creating a fresh “Win32 Application” project and moving the files in is a quick fallback if settings become confusing.

Recommended Answers

All 7 Replies

Where are the code tags?
Where is main() defined (unresolved external symbol _main)
Did you link with the debug library?

Make sure you created a Win32 project and not a console project (how to do that depends on your IDE)

when I include this C++ source file into a "Win32 Application" project, it works;
when include it into a "Win32 Console Application" project, it fails;
my question is how to change the settings of the latter to make it work, without creating a new project of Wind32 Application.
I tried include the macro _WINDOWS in the setting->c/c++->preprocess definitions , but it did not work.

Visual Studio? Try Linker->System->Subsystem (/SUBSYSTEM:WINDOWS)

Although I can't guarantee there aren't other options that are normally set for a Win32 project, and I don't know why you would want to do this.

Why do you want a console application to be windowed? I mean, there are ways, but it seems silly.

// Windows Programming Tutorial Series
      #include "stdafx.h"
      #include <windows.h>
      int WINAPI
      WinMain(HINSTANCE hInst,
      HINSTANCE hPrevInstance,
      LPSTR lpCmdLine,
      int nCmdShow)
      {
      MessageBox (NULL, "Hello World! This is my first WIN32 program",
      "Lesson 1", MB_OK);
      return 0;
      }

I think I may know your problem, and for a million dollars ill solve it for you :D.

you forgot a ";"

Guess where?

// Windows Programming Tutorial Series
#include "stdafx.h"
#include <windows.h>
int WINAPI
WinMain(HINSTANCE hInst,
HINSTANCE hPrevInstance,

The lack of a ";" is messing up your winmain. that's why it has a problem.

Least... im pretty sure. Beginner at C++ myself but im good at PHP and that would create a problem in most languages, if im right.

commented: 3 years too late - FAIL -4

@ ookamioni The problem OP is having is he is trying to make a win32 app work as a win32 console. In a console you generally have int main() not WinMain(). He is not missing a ";" anywhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.