Advertisement

Confusion

Started by September 27, 2002 05:08 AM
1 comment, last by SteveBe 22 years, 1 month ago
Following several recommendations on here I bought the Petzold Windows Programming book 5th Edition. I''m at the end of the second chapter and have typed in the code shown in the example. My understanding is that the book is written mainly for use with MSVC 6.0 which I''m using. The code doesn''t compile and I don''t understand the errors I''m getting. However, I tried compiling the code in Dev-C++ and it compiled and ran without error. I don''t understand why. Can someone help. Source and errors listed below:
  

/*-----------------------------------------------------
   SCRNSIZE.C -- Displays screen size in a message box
                 (c) Charles Petzold, 1998
  -----------------------------------------------------*/

#include <windows.h>
#include <tchar.h>     
#include <stdio.h>     

int CDECL MessageBoxPrintf (TCHAR * szCaption, TCHAR * szFormat, ...)
{
     TCHAR   szBuffer [1024] ;
     va_list pArgList ;

          // The va_start macro (defined in STDARG.H) is usually equivalent to:

          // pArgList = (char *) &szFormat + sizeof (szFormat) ;


     va_start (pArgList, szFormat) ;

          // The last argument to wvsprintf points to the arguments


     _vsntprintf (szBuffer, sizeof (szBuffer) / sizeof (TCHAR), 
                  szFormat, pArgList) ;

          // The va_end macro just zeroes out pArgList for no good reason


     va_end (pArgList) ;

     return MessageBox (NULL, szBuffer, szCaption, 0) ;
}

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow) 
{
     int cxScreen, cyScreen ;

     cxScreen = GetSystemMetrics (SM_CXSCREEN) ;
     cyScreen = GetSystemMetrics (SM_CYSCREEN) ;

     MessageBoxPrintf (TEXT ("ScrnSize"), 
                       TEXT ("The screen is %i pixels wide by %i pixels high."),
                       cxScreen, cyScreen) ;
     return 0 ;
}

  
Compiling... rough.cpp Linking... LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main Debug/rough.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. rough.exe - 2 error(s), 0 warning(s) Why am I getting this error, and why is it compiling in Dev-Cpp and not MSVC?
Hmmm...

This error occurs when there is no body for a declared function.

Example:

void Foo(void);

void Bar(void)
{
Foo()
}

Error: unresolved...Foo()

In normal cases main() should be defined in the c-runtime-lib
(LIBC.LIB) (the D is for DEBUG,there is also MT-MultiThreaded).

How can this be solved?

Make sure you link the right libs.
In a debug build this is LIBCD, in release LIBC.

Have you included this code in a Win32-Project in VC++?
If you do that then VC++ should do the proper code setup for you.

Am I right? Has someone another idea?

Greetings
There are 10 kinds of people,those who understand binaryand those who not.
Advertisement
Martin - thanks.

I was being a complete thicko and doing it in a console application instead of a win32 application.

This topic is closed to new replies.

Advertisement