You are here:

C++/Visual C error

Advertisement


Question
hi..
I'm working with visual c   since a short period specailly with windows programming and I'm programming a code for simple window design but the following error occur:
error LNK2001: unresolved external symbol "long __stdcall WindowFunc(struct HWND__ *,unsigned int,unsigned int,long)" (?WindowFunc@@YGJPAUHWND__@@IIJ@Z)
can you please show me what to do

Answer
Possibly. I cannot say for sure but can give some general advice and make a more specific guess at your problem.

General advice:
------------------
This is a linker error. The linker (which links all the individual modules of a program together) cannot find the item named in the error message in the object files and libraries (.obj and .lib files) used to link together into an executable. However the item named in the error message was referred to from one or more other modules and is needed so as to be able to complete the creation of an executable.

Note: the details of which object and library file names are passed to the linker may not be obvious if you are using a project inside an IDE such as Visual Studio. You should check carefully the project settings or properties.

So, provide the linker with a compiled definition for the unresolved function symbol (i.e. the function WindowFunc). That is, link against the library or object code file containing the definition of this function. If you have not written it then write it and ensure it is compiled and the name of the associated .obj (or .lib) file is added to the list of files to be used by the linker (if it is not already listed).

More specific advice:
------------------------
My guess is that WindowFunc is the name of a Windows window message handling callback function, which you either have not written or not linked against (maybe you placed it into a source file and forgot to add this source file to the project files). You may even have declared this function then forgotten to implement it, thus:

#include <Windows.h>

LPCTSTR WindowClassName = TEXT("AppMainWndClass");

//###############################################

// WindowFunc callback declared here.
// Defined (implemented) elsewhere...

LRESULT CALLBACK WindowFunc
( HWND hwnd
,  UINT uMsg
,  WPARAM wParam
,  LPARAM lParam
);

//###############################################

ATOM InitApp( HINSTANCE  hInstance )
{
   WNDCLASSEX wndClass;
   wndClass.cbSize = sizeof( WNDCLASSEX );
   wndClass.cbClsExtra = 0;
   wndClass.cbWndExtra = 0;
   wndClass.hCursor = NULL;
   wndClass.hIcon = NULL;
   wndClass.hIconSm = NULL;
   wndClass.lpszMenuName = NULL;
   wndClass.style = 0;
   wndClass.hbrBackground =
         reinterpret_cast<HBRUSH>(COLOR_WINDOW);
   wndClass.lpfnWndProc = WindowFunc;
   wndClass.lpszClassName = WindowClassName;
   wndClass.hInstance = hInstance;
   return ::RegisterClassEx( &wndClass );
}

// Etc...

In the above code I have declared that somewhere there is a WindowFunc callback to be used to handle messages for windows of the WindowClassName class. However if there is nowhere in the code that defines the WindowFunc function then obviously it cannot be used (called or have its address taken). The line:

   wndClass.lpfnWndProc = WindowFunc;

takes the address of the WindowFunc function.

When the linker comes to resolve this address however it cannot find the entry for the definition for the WindowFunc function (because it does not exist) and so fails to create an executable, issuing the unresolved external error message.

As mentioned above, as far as the linker is concerned not existing is the same as not being passed the file name of the object or library file containing the object code for the item in question.  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Ralph McArdell

Expertise

I am a software developer with more than 15 years C++ experience and over 25 years experience developing a wide variety of applications for Windows NT/2000/XP, UNIX, Linux and other platforms. I can help with basic to advanced C++, C (although I do not write just-C much if at all these days so maybe ask in the C section about purely C matters), software development and many platform specific and system development problems.

Experience

My career started in the mid 1980s working as a batch process operator for the now defunct Inner London Education Authority, working on Prime mini computers. I then moved into the role of Programmer / Analyst, also on the Primes, then into technical support and finally into the micro computing section, using a variety of 16 and 8 bit machines. Following the demise of the ILEA I worked for a small company, now gone, called Hodos. I worked on a part task train simulator using C and the Intel DVI (Digital Video Interactive) - the hardware based predecessor to Indeo. Other projects included a CGI based train simulator (different goals to the first), and various other projects in C and Visual Basic (er, version 1 that is). When Hodos went into receivership I went freelance and finally managed to start working in C++. I initially had contracts working on train simulators (surprise) and multimedia - I worked on many of the Dorling Kindersley CD-ROM titles and wrote the screensaver games for the Wallace and Gromit Cracking Animator CD. My more recent contracts have been more traditionally IT based, working predominately in C++ on MS Windows NT, 2000. XP, Linux and UN*X. These projects have had wide ranging additional skill sets including system analysis and design, databases and SQL in various guises, C#, client server and remoting, cross porting applications between platforms and various client development processes. I have an interest in the development of the C++ core language and libraries and try to keep up with at least some of the papers on the ISO C++ Standard Committee site at http://www.open-std.org/jtc1/sc22/wg21/.

Education/Credentials

©2016 About.com. All rights reserved.