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.

richardec
  • 14,202
  • 6
  • 23
  • 49
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

1 Answers1

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