2

This is the second day I'm struggling with this nasty issue. Test code I try to compile:

#include <windows.h>
#include <setupapi.h>
#include <initguid.h>
#include <devguid.h>

int main(void)
{
    HDEVINFO device_info_set = SetupDiGetClassDevs(
        (const GUID *) &GUID_DEVCLASS_PORTS,
        NULL,
        NULL,
        DIGCF_PRESENT);
        
    return 0;
}

And mingw output:

loleq@loleq-Pc MINGW32 /c/Users/loleq/Downloads/openpst/openpst/sahara/lib/libopenpst/lib/serial/examples
$ gcc --version
gcc.exe (i686-posix-dwarf-rev0, Built by MinGW-W64 project) 5.3.0
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


loleq@loleq-Pc MINGW32 /c/Users/loleq/Downloads/openpst/openpst/sahara/lib/libopenpst/lib/serial/examples
$ gcc -lsetupapi -lhid  test.c
C:\opt\msys2_64\tmp\ccJZHvjA.o:test.c:(.text+0x36): undefined reference to `_imp__SetupDiGetClassDevsA@16'
collect2.exe: error: ld returned 1 exit status

Host system is Win7 64bit, I didn't install any additional libraries, i.e. DDK or so... I believe missing reference should be handled by libsetupapi.a (-lsetupapi) which of course presents in MinGW libraries. Any suggestions? BTW: MinGW I'm using right now is provided by Qt.

loleq
  • 21
  • 1
  • 2
  • I remember reading something a while back that suggested to put the libraries after specifying the source file (might be a MinGW thing), so you could probably try `gcc test.c -lsetupapi -lhid`. – John Mark Gabriel Caguicla Sep 17 '17 at 09:47
  • Wow you are my hero sir. Looks like it really solves the issue. Thanks John. – loleq Sep 17 '17 at 10:26
  • Glad to hear that, I'll go ahead and submit it as an answer. – John Mark Gabriel Caguicla Sep 17 '17 at 10:35
  • Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Mike Kinghan Sep 18 '17 at 18:44
  • Answered by [Your linkage consumes libraries before the object files that refer to them](https://stackoverflow.com/a/43305704/1362568) – Mike Kinghan Sep 18 '17 at 18:46

2 Answers2

4

The problem seems to be due to the parameter ordering when invoking the MinGW gcc binary.

I've read somewhere a while ago that libraries need to be specified last when using MinGW, as such your command should be:

$ gcc test.c -lsetupapi -lhid
0

I am facing similar issue. After putting the libraries at the end of command, I am a getting new error now "undefined reference to `GUID_DEVCLASS_PORTS'". I have all the includes in my code as above.

I looked at my devguid.h and could see GUID_DEVCLASS_PORTS being defined as extern const, but I could not find it's definition anywhere.

ifdef __cplusplus
extern "C" {
#endif
extern const GUID GUID_DEVCLASS_WCEUSBS;
extern const GUID GUID_DEVCLASS_USB;
extern const GUID GUID_DEVCLASS_PNPPRINTERS;
extern const GUID GUID_DEVCLASS_DOT4;
extern const GUID GUID_DEVCLASS_DOT4PRINT;
extern const GUID GUID_DEVCLASS_CDROM;
extern const GUID GUID_DEVCLASS_COMPUTER;
extern const GUID GUID_DEVCLASS_DISKDRIVE;
extern const GUID GUID_DEVCLASS_DISPLAY;
extern const GUID GUID_DEVCLASS_FDC;
extern const GUID GUID_DEVCLASS_HDC;
extern const GUID GUID_DEVCLASS_KEYBOARD;
extern const GUID GUID_DEVCLASS_MEDIA;
extern const GUID GUID_DEVCLASS_MODEM;
extern const GUID GUID_DEVCLASS_MONITOR;
extern const GUID GUID_DEVCLASS_MOUSE;
extern const GUID GUID_DEVCLASS_MTD;
extern const GUID GUID_DEVCLASS_MULTIFUNCTION;
extern const GUID GUID_DEVCLASS_NET;
extern const GUID GUID_DEVCLASS_NETCLIENT;
extern const GUID GUID_DEVCLASS_NETSERVICE;
extern const GUID GUID_DEVCLASS_NETTRANS;
extern const GUID GUID_DEVCLASS_PCMCIA;
extern const GUID GUID_DEVCLASS_PORTS;
Nirmala
  • 21
  • 5
  • 1
    Try: 'gcc test.c -lsetupapi -lhid -luuid' – loleq Jun 10 '22 at 05:55
  • Also, make sure these libraries are available in your mingw instance, i.e. search your MinGW installation path for: libsetupapi.a libhid.a libuuid.a `find /mingw64/lib -name "libsetupapi.a" /mingw64/lib/libsetupapi.a` – loleq Jun 10 '22 at 06:04
  • @loleq Thank you. 'gcc test.c -lsetupapi -lhid -luuid ' worked and I was able to build the file successfully. I was naively trying by adding -ldevguid at the end since this file had the required definition. I am wondering what documentation could have helped me if I would not have got my answer from stackoverflow. (An electronics engineer learning bit of software). – Nirmala Jun 14 '22 at 03:34
  • Don't forget C/C++ code have a concept of declaration and definition, usually extern declarations are enclosed in *.h files and can repeat in many places, whereas definition of shared instance should happen only once and usually it is done in *.c file. Nowoadays libraries contain built in information about its interfaces, this information can be extracted by tools like objdump or other third party solutions. In this particular case I just looked for string `GUID_DEVCLASS_PORTS` within *.a files and I got exactly one hit - libuuid.a. – loleq Jun 15 '22 at 05:48
  • Unfortunately I can't comprehend why in one case gcc has to be explicitly informed about -luuid and in other not... I was able to build this code either with or without this flag. – loleq Jun 15 '22 at 05:49