I'm trying to use InetPtonW
:
if(InetPtonW(AF_INET, argv[1], &ThisSenderInfo.sin_addr)<=0) {
return 1;
}
However I get the following message when compiling:
warning: implicit declaration of function 'InetPtonW' [-Wimplicit-function-declaration]
undefined reference to `InetPtonW'
collect2.exe: error: ld returned 1 exit status
I've read the documentation located here and I've followed everything but still can't get it to work.
• I'm compiling with Ws2_32 library gcc test.c -o test -lws2_32
using MinGW
• I've included the needed header files #include <ws2tcpip.h>
and #include <windows.h>
• I've tried using InetPton
but it returns the same error
• Running on Windows 10
One of the popular causes for the error undefined reference could be some name issue of a function. So, in this example, we will be seeing how an error could be generated and resolved using the function name. We will be creating a new “exp.c” file to add our C code to it within the shell terminal.
The error: undefined reference to function show () has appeared on the terminal shell as predicted. To solve this error, simply open the file and make the name of a function the same in its function definition and function call. So, we used to show (), i.e., small case names to go further.
When UNICODE or _UNICODE is defined, InetPton is defined to InetPtonW, the Unicode version of this function. The pszAddrString parameter is defined to the PCWSTR data type. When UNICODE or _UNICODE is not defined, InetPton is defined to InetPtonA, the ANSI version of this function. The ANSI version of this function is always defined as inet_pton.
The InetPton function does not require that the Windows Sockets DLL be loaded to perform conversion of a text string that represents an IP address to a numeric binary IP address.
I recall running into this exact issue some many months ago. @alk's comment points to a question whose accepted answer feels very similar to what fixed it for me.
You should be able to #define
a version macro (or two) before your #include
lines to fix it.
While I feel strongly that the aforementioned answer is correct, I'll update this answer later today when I can verify.
The code I was referencing above doesn't have InetPtonW
in it anymore but it had the necessary #define
s in it. Here's a brief example that compiles on my machine (win10/mingw64/gcc 8.2.0):
Z:\Some\Directory>gcc test.c -o test -lmswsock -lws2_32
#define NTDDI_VERSION NTDDI_VISTA
#define WINVER _WIN32_WINNT_VISTA
#define _WIN32_WINNT _WIN32_WINNT_VISTA
#include <winsock2.h>
#include <Ws2tcpip.h>
#include <stdio.h>
/* This is "test.c", please pardon the lack of error checking. */
int main(void) {
BYTE ipbuf[4] = {0};
WSADATA wsa;
WSAStartup(MAKEWORD(2,2), &wsa);
printf("%d: ", InetPtonW(AF_INET, L"127.0.0.1", &ipbuf));
for(int i = 0; i < 4; ++i)
printf("%hhu.", ipbuf[i]);
WSACleanup();
}
Output should look like:
Z:\Some\Directory>gcc test.c -o test -lmswsock -lws2_32
Z:\Some\Directory>test
1: 127.0.0.1.
Z:\Some\Directory>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With