Solved

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

Posted on 2011-02-23
3
720 Views
Last Modified: 2012-05-11
Hi. I'm trying to learn DirectX by following this tutorial, http://www.directxtutorial.com/Tutorial11/A-A/AA2.aspx, 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()
    MessageBox(NULL,
               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..

Error:
------ 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!
0
Comment
Question by:rationalboss
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
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).
0
 
LVL 8

Author Comment

by:rationalboss
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!
0
 
LVL 86

Accepted Solution

by:
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".
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

839 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