[Solved]Linking to some functions of windows sockets api



  • Hello,
    I have a Qt project with libmodbus in it.
    On Linux it compiles and links ok , but on Windows there are linking errors that I try to resolve and ask here perhaps somebody
    get the same problem:
    I defined
    @
    #define WINVER = 0x0501@

    There is a C code that uses some functions for tcp/ip communication
    I included: <Ws2tcpip.h> <winsock.h><winsock2.h><w32api.h><winnetwk.h><ws2spi.h><wtsapi32.h>
    also
    and also added to Qt .pro file next library
    @win32:LIBS += -lsetupapi
    -Ws_2_32.lib@

    The compiler is mingw32

    I get next linking errors:

    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x128): undefined reference to setsockopt@20' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x160): undefined reference tosetsockopt@20'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x1b7): undefined reference to getaddrinfo@16' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x1ec): undefined reference tosocket@12'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x214): undefined reference to connect@12' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x22c): undefined reference tofreeaddrinfo@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x244): undefined reference to closesocket@4' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x28b): undefined reference tosocket@12'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x2b9): undefined reference to htons@4' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x2c6): undefined reference toinet_addr@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x2e4): undefined reference to connect@12' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x314): undefined reference toclosesocket@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x5e0): undefined reference to select@20' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x673): undefined reference toaccept@12'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x6a6): undefined reference to closesocket@4' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x6de): undefined reference toaccept@12'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x707): undefined reference to inet_ntoa@4' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x72f): undefined reference toclosesocket@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x7b0): undefined reference to getaddrinfo@16' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x813): undefined reference tosetsockopt@20'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x822): undefined reference to closesocket@4' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x855): undefined reference tosocket@12'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x88e): undefined reference to bind@12' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x89d): undefined reference toclosesocket@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x8cb): undefined reference to listen@8' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x8dd): undefined reference tofreeaddrinfo@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x907): undefined reference to freeaddrinfo@4' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x918): undefined reference toclosesocket@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x966): undefined reference to socket@12' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x9a2): undefined reference tosetsockopt@20'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x9ca): undefined reference to htons@4' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x9ea): undefined reference tobind@12'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0x9ff): undefined reference to listen@8' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0xa18): undefined reference toclosesocket@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0xa57): undefined reference to recv@16' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0xa9e): undefined reference torecv@16'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0xac1): undefined reference to shutdown@8' ./release\modbus-tcp.o:modbus-tcp.c:(.text+0xacf): undefined reference toclosesocket@4'
    ./release\modbus-tcp.o:modbus-tcp.c:(.text+0xb92): undefined reference to `send@16'
    It seems mingw could not link to windows socket functions that are in the list.
    What header to include, what library to add to .pro file so as the linking will succeed?
    Thank you very much
    Awaiting for your help.



  • mingw can't link .lib files.
    you should try :

    @
    win32:LIBS += -lsetupapi
    -lWs_2_32
    @



  • Also, please not that the lib is named "ws2_32", so you have to add to your .pro file:

    @
    LIBS += -lws2_32
    @



  • yes,
    there should be added -lws2_32 and not -Ws_2_32.lib to project file LIBS section.
    Thank you. It links. Solved!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.