Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to `_imp__SetupDiGetClassDevsA@16' (even with -lsetupapi)

Tags:

c++

c

mingw

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.

like image 662
loleq Avatar asked Dec 02 '25 07:12

loleq


1 Answers

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
like image 167
John Mark Gabriel Caguicla Avatar answered Dec 04 '25 21:12

John Mark Gabriel Caguicla