Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Why am I getting this LNK2001 unresolved external symbol error?

Posted on 2003-11-06
9
Medium Priority
?
635 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 520 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

609 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