Solved

Why am I getting this LNK2001 unresolved external symbol error?

Posted on 2003-11-06
9
625 Views
Last Modified: 2008-02-01
This is the error that I am getting:

Compiling...
yeahboye.cpp
Linking...
yeahboye.obj : error LNK2001: unresolved external symbol "int __cdecl EnumDisplayDevices(char const *,unsigned long,struct _DISPLAY_DEVICE *,unsigned long)" (?EnumDisplayDevices@@YAHPBDKPAU_DISPLAY_DEVICE@@K@Z)
Debug/yeahboye.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.
Creating browse info file...

yeahboye.exe - 2 error(s), 0 warning(s)

for this code:

typedef struct _DISPLAY_DEVICE {
  DWORD cb;
  WCHAR DeviceName[32];
  WCHAR DeviceString[128];
  DWORD StateFlags;
} DISPLAY_DEVICE, *PDISPLAY_DEVICE;
DISPLAY_DEVICE dd;
dd.cb = sizeof(DISPLAY_DEVICE);


BOOL EnumDisplayDevices(
  LPCTSTR lpDevice,                // device name
  DWORD iDevNum,                   // display device
  PDISPLAY_DEVICE lpDisplayDevice, // device information
  DWORD dwFlags                  // reserved
);

 if (( EnumDisplayDevices(NULL,0,&dd,0) ) != 0)
    printf("%s", dd.DeviceString);

I have both winuser.h and wingdi.h included.  What is wrong with my code and/or do you have any suggestions to resolve this error?
0
Comment
Question by:KSpin
  • 3
  • 3
  • 2
9 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 9694039
>>What is wrong with my code

You are apparently using a C++ compilation mode (the name mangling suggests that). Either name your file .c instead of .cpp or force C linkage by using

#ifdef __cplusplus
extern "C" {
#endif
#include <winuser.h>
#include <wingdi.h>
#ifdef __cplusplus
}
#endif

Of course, it would be easier to just

#include <windows.h>

:o)
0
 

Author Comment

by:KSpin
ID: 9694387
I changed it to a .c, but I still got the same error.  Also, this code:

#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
 is declared in the winuser.h file which I have done a #include on.  I have also included wingdi.h as well as windows.h.  Any more suggestions?
0
 
LVL 86

Expert Comment

by:jkr
ID: 9694430
>>I have also included wingdi.h as well as windows.h.

That is not necessary.

#include <windows.h>

is enough and will probably save you from other problems, too.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:KSpin
ID: 9694548
Removed wingdi.h, winuser.h............still have the same problem.

Could it have anything with EnumDisplayDevices being an old command?  Any more suggestions?
0
 
LVL 6

Expert Comment

by:marconovaro
ID: 9694678

The problem you are facing is a LINKER problem.

Since you put the prototype of the function in the .C file, the compiler is ok (i.e. it acn find the types of he parameters, and determine if the parameters you are using are of the correct type).

But, to create the .EXE, the linker needs the body of the function "EnumDisplayDevices" (i.e. it needs to jump somewhere in the code to execute something when you call that function).

This means: you need an appropriate library (DLL, or LIB, depends on where is the function itself). Look in MSDN help: it will tell you what libraries to include in the project.

HTH
M
0
 

Author Comment

by:KSpin
ID: 9694783
I did this....have usr32.lib included

  Windows NT/2000 or later: Requires Windows 2000 or later.
  Windows 95/98/Me: Requires Windows 98 or later.
  Header: Declared in Winuser.h; include Windows.h.
  Library: Use User32.lib.
  Unicode: Implemented as Unicode and ANSI versions on Windows 2000.
0
 
LVL 6

Accepted Solution

by:
marconovaro earned 130 total points
ID: 9694870

Ok. So, what you need to do is include that library in your project

1. menu Project -> Properties
2. find the linker properties, and set that library in the list of libraries
3. recompile

Good luck :-)
M.
0
 
LVL 6

Expert Comment

by:marconovaro
ID: 9695324

I usually do not complain about question grade, but NOT in this case!

WHY did you give me a "C" grade? It seems that my answer works: isn't it?
Please reply.
M.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert image to byte array 8 203
Need example 5 116
C++ :Change value from  DisableCMD registry 4 60
Why  my code (program) build with old compiler? 11 49
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

832 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