• C

Why am I getting this LNK2001 unresolved external symbol error?

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?
KSpinAsked:
Who is Participating?
 
marconovaroCommented:

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
 
jkrCommented:
>>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
 
KSpinAuthor Commented:
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
Choose an Exciting Career in Cybersecurity

Help prevent cyber-threats and provide solutions to safeguard our global digital economy. Earn your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

 
jkrCommented:
>>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
 
KSpinAuthor Commented:
Removed wingdi.h, winuser.h............still have the same problem.

Could it have anything with EnumDisplayDevices being an old command?  Any more suggestions?
0
 
marconovaroCommented:

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
 
KSpinAuthor Commented:
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
 
marconovaroCommented:

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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.