Solved

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

Posted on 2011-02-23
3
711 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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now