MinGW – Error – “undefined reference to `__imp_WSAStartup'”

Background

Trying to compile netcat ( nc ) using minGW.

 

Lineage

  1. NetCat – Visual Studio – Source Code Compilation
    Link
  2. Netcat on MS Windows – Sample Usage
    Link

Error

Error – Text


undefined reference to `__imp_WSAGetLastError'
undefined reference to `__imp_WSAGetLastError'
undefined reference to `__imp_shutdown'
undefined reference to `__imp_closesocket'
undefined reference to `__imp_inet_addr'
undefined reference to `__imp_gethostbyname'
undefined reference to `__imp_WSAGetLastError'
undefined reference to `__imp_inet_ntoa'
undefined reference to `__imp_gethostbyaddr'
undefined reference to `__imp_WSAGetLastError'
undefined reference to `__imp_inet_ntoa'
undefined reference to `__imp_gethostbyaddr'
undefined reference to `__imp_WSAGetLastError'
undefined reference to `__imp_gethostbyname'
undefined reference to `__imp_WSAGetLastError'
undefined reference to `__imp_htons'
undefined reference to `__imp_getservbyport'
undefined reference to `__imp_ntohs'
undefined reference to `__imp_getservbyname'
undefined reference to `__imp_ntohs'
 undefined reference to `__imp_WSASetLastError'
 undefined reference to `__imp_socket'
 undefined reference to `__imp_socket'
 undefined reference to `__imp_setsockopt'
 undefined reference to `__imp_htons'
 undefined reference to `__imp_bind'
 undefined reference to `__imp_inet_ntoa'
 undefined reference to `__imp_inet_ntoa'
 undefined reference to `__imp_htons'
 undefined reference to `__imp_connect'
 undefined reference to `__imp_WSASetLastError'
 undefined reference to `__imp_WSAGetLastError'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_WSASetLastError'
 undefined reference to `__imp_listen'
 undefined reference to `__imp_getsockname'
 undefined reference to `__imp_inet_ntoa'
 undefined reference to `__imp_ntohs'
 undefined reference to `__imp_recvfrom'
 undefined reference to `__imp_connect'
 undefined reference to `__imp_accept'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_getsockname'
 undefined reference to `__imp_inet_ntoa'
 undefined reference to `__imp_ntohs'
 undefined reference to `__imp_inet_ntoa'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_send'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_send'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_WSASetLastError'
 undefined reference to `__imp_select'
 undefined reference to `__imp_WSAGetLastError'
 undefined reference to `__imp_WSAGetLastError'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_WSASetLastError'
 undefined reference to `__WSAFDIsSet'
 undefined reference to `__imp_recv'
 undefined reference to `__imp_send'
 undefined reference to `__WSAFDIsSet'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_WSAGetLastError'
 undefined reference to `__imp_shutdown'
 undefined reference to `__imp_closesocket'
 undefined reference to `__imp_WSACleanup' 

TroubleShooting

Microsoft

Docs

Docs / Windows / Windows Sockets 2 / Winsock.h / WSAGetLastError function

Link

Image

winsock_01_20200303_1018PM

Textual
  1. Header
    • winsock.h ( include Winsock2.h )
  2. Library
    • Ws2_32.lib
  3. Dll
    • Ws2_32.dll

 

Remediation

Outline

  1. Adding ws2_32 to list of libraries referenced during the build

Sample


setlocal

rem dadeniji
rem SET COMPILER=c:\MinGW\bin\gcc.exe

set _compilerFolder=C:\mingw\x86_64\8.1.0\posix\seh\mingw64\bin
set _compilerBinary=gcc.exe
set COMPILER=%_compilerFolder%\%_compilerBinary%

rem dadeniji
rem SET LIB_DIR=c:\MinGW\lib
SET LIB_DIR=C:\mingw\x86_64\8.1.0\posix\seh\mingw64\lib

rem dadeniji
set PATH=%PATH%;%_compilerFolder%

@REM	not needed? SET COMPILE_OPTIONS=-c -DWIN32 -DNDEBUG -D_CONSOLE -DTELNET
SET COMPILE_OPTIONS=-c

if exist *.o del *.o

if exist nc.exe del nc.exe

"%COMPILER%" %COMPILE_OPTIONS% getopt.c

"%COMPILER%" %COMPILE_OPTIONS% doexec.c

"%COMPILER%" %COMPILE_OPTIONS% netcat.c

"%COMPILER%" getopt.o doexec.o netcat.o --output nc.exe -lkernel32 -luser32 -lwinmm -lws2_32

endlocal

Source Code Control

GitHub

Gist

netcat on MS Windows
Link

Summary

When we initially downloaded the source code for netcat on MS Windows, we used Microsoft’s Visual Studio.Net to compile it.

Since then downloaded MinGW and gained some familiarity with that tool, as well.

Thankfully, able to compile against the MinGW tool with small modifications.

 

References

  1. Microsoft
    • Docs
      • Docs / Windows / Windows Sockets 2 / Winsock.h / WSAGetLastError function
        Link
  2.  StackOverflow
    • undefined reference to `__imp_WSACleanup’
      Link

One thought on “MinGW – Error – “undefined reference to `__imp_WSAStartup'”

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s