Solved

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

Posted on 2011-02-23
3
721 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maze travler 6 66
convert char array to number in c 5 91
Safe conversion? 4 76
c++ aadding data to a list box vs2005 mfc project 7 14
Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

730 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