views:

1290

answers:

2

Hello,

I am using MinGW compiler on Windows to compile my C++ application with sockets. My command for linking looks like:

g++.exe -Wall -Wno-long-long -pedantic -lwsock32 -o dist/Windows/piskvorky { there are a lot of object files }

and I have also tried

    g++.exe -Wall -Wno-long-long -pedantic -lws2_32 -o dist/Windows/piskvorky { there are a lot of object files }

but in both case I get this error:

build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0x33): undefined reference to `closesocket@4'

build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0xd0): undefined reference to `send@16'

build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0xf6): undefined reference to `send@16'

build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0x1ee): undefined reference to `recv@16'

build/Windows/MinGW-Windows/src/utils/tcpunit.o:tcpunit.cpp:(.text+0x215): undefined reference to `recv@16'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x13c): undefined reference to `WSAStartup@8'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x184): undefined reference to `WSAStartup@8'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x1a5): undefined reference to `closesocket@4'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x1ad): undefined reference to `WSACleanup@0'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x1cb): undefined reference to `closesocket@4'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x1d3): undefined reference to `WSACleanup@0'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x6a7): undefined reference to `socket@12'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x6d4): undefined reference to `htons@4'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x6fe): undefined reference to `bind@12'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x724): undefined reference to `listen@8'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x7f0): undefined reference to `gethostbyaddr@12'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x83c): undefined reference to `socket@12'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x86f): undefined reference to `htons@4'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x8b5): undefined reference to `connect@12'

build/Windows/MinGW-Windows/src/utils/tcpdevice.o:tcpdevice.cpp:(.text+0x9c6): undefined reference to `accept@12'

Do you have any ideas where the problem can be, please?

Thank you

A: 

Some things to check:

  • Does MinGW actually find the winsock library? (It seems so, since there's no explicit error saying otherwise.) If it does not, try to supply an additional library search path using -L.

  • Did you compile the winsock library source yourself, also with MinGW? If the answer is "no", and the source code is available, that's what you might have to do, so that MinGW will recognise the exported symbols in the library object file.

stakx
The winsock2 library is Microsoft's - they don't give you the source.
anon
+3  A: 

Put the -lws2_32 AFTER the list of object files - GCC searches libraries and object files in the order they appear on the command line.

anon
Thanks a lot for your advice
Gaim