Solved

Why am I getting this LNK2001 unresolved external symbol error?

Posted on 2003-11-06
9
616 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Which checksum is this? 7 136
SCANF - LIMIT THE NUMBER OF CHARARACTERS 1 58
why "." vs "->" 23 116
C Language combined operators 28 102
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files 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.

743 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

14 Experts available now in Live!

Get 1:1 Help Now