Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2011-02-23
3
Medium Priority
?
730 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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

972 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