C++ - Basic Windows Can't Compile at MS Visual C++

Posted on 2011-02-23
Last Modified: 2012-05-11
Hi. I'm trying to learn DirectX by following this tutorial,, but I can't pass its prerequisite, creating a Basic Window in MS Visual C++.
Here is the code.

// include the basic windows header file
#include <Windows.h>
#include <WindowsX.h>

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
    // create a "Hello World" message box using MessageBox()
               L"Hello World!",
               L"Just another Hello World program!",
               MB_ICONEXCLAMATION | MB_OK);

    // return 0 to Windows
    return 0;

Open in new window

When I press F5 (build), I get these errrors..

------ Build started: Project: Project1, Configuration: Debug Win32 ------
main.obj : error LNK2028: unresolved token (0A000044) "extern "C" int __stdcall MessageBoxW(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) referenced in function "extern "C" int __cdecl MessageBox(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBox@@$$J0YAHPAUHWND__@@PB_W1I@Z)
main.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall MessageBoxW(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) referenced in function "extern "C" int __cdecl MessageBox(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBox@@$$J0YAHPAUHWND__@@PB_W1I@Z)
C:\Users\Johnny\documents\visual studio 2010\Projects\Project1\Debug\Project1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Open in new window

How do I make this work? Thanks!
Question by:rationalboss
  • 2
LVL 86

Expert Comment

ID: 34963155
That is a linker error, the compilation seems to run OK. Somehow the default libraries (in your case 'user32.lib') seem not to be included in your build. Try to either add that library to your project via Visual Studio or add the line

#pragma comment(lib,"user32.lib")

to your code (which basically achieves the same).

Author Comment

ID: 34963237
Thanks. Adding #pragma comment(lib,"user32.lib") at the top of my code works. but I would also like to know, how do I add that library in my project via Visual Studio? In what directory can I find user32.lib? Can you give me more specific directions (a real noob here)? Thanks!
LVL 86

Accepted Solution

jkr earned 500 total points
ID: 34963331
All these libraries usually reside in the directories that are listed under "Tools|Options|Projects and Solutions|VC++ Directories" (Set "Show Directories for:" to "Library Files").

To add libs using VS, open your project's properties (ALT+F7), go to "Configuration Properties|Linker|Input" and enter the lib(s) under "Additional Dependencies". If the lib you want to use does not reside in one of the standard directories or your project's build directory (Debug or Release), you can set that dirctory under "Configuration Properties|Linker|General" as "Additional Library Directories".

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question