Solved

Why am I getting this LNK2001 unresolved external symbol error?

Posted on 2003-11-06
9
619 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
 

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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using unmanaged DLL from managed application 14 290
Detect CR LF to each line 12 152
Socket Programming (Unix) 8 118
c++ getting the first 10 characters of a char* string 11 89
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

947 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now