admin管理员组

文章数量:1122846

I am a student starting to learn socket programming using C, and when I am trying to create a client-server environment I am facing this error while compiling:

if (inet_pton(AF_INET6 , "10.163.3.30", &serv_addr.sin_addr) <= 0) {
         ^~~~~
warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-declaration]
     if (inet_pton(AF_INET6 , "10.163.3.30", &serv_addr.sin_addr) <= 0) {
         ^~~~~
C:\Users\xtsub\AppData\Local\Temp\ccWTMEPx.o:client.c:(.text+0xe9): undefined reference to `inet_pton'

Below is my code for my client.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define PORT 8080  // Port number to connect to

int main() {
    WSADATA wsaData;
    SOCKET sock;
    struct sockaddr_in serv_addr;
    char buffer[1024] = {0};

    // Initialize Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        printf("WSAStartup failed\n");
        return 1;
    }

    // 1. Create socket
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET) {
        printf("Socket creation failed: %d\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // 2. Define server address
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // Convert IPv4 address from text to binary form
    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
        printf("Invalid address or address not supported\n");
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    // 3. Connect to the server
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("Connection to server failed: %d\n", WSAGetLastError());
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    // 4. Send data to the server
    char *message = "Hello from client";
    send(sock, message, strlen(message), 0);
    printf("Sent to server: %s\n", message);

    // 5. Receive response from the server
    int valread = recv(sock, buffer, sizeof(buffer), 0);
    if (valread > 0) {
        buffer[valread] = '\0';  // Null-terminate the received string
        printf("Received from server: %s\n", buffer);
    } else {
        printf("No response from server or error occurred\n");
    }

    // 6. Close the socket
    closesocket(sock);

    // Cleanup Winsock
    WSACleanup();
    
    return 0;
}

I have another code for my server, server.c setup and it works correctly so I think maybe it is not a compiler error. (Compiled successfully and when prompted with ./server shows message "Server is listening on port 8080..."). I have search on this error and it seems the function is not recognized by the corresponding library header is in my MinGw folder. I asked a teaching assistant but unfortunately it is still not resolved for now. I will appreciate it if anyone knows the solution. I am not proficient at all in this so please bear with it if there are any errors/information lacking.

What I tried:

  • replacing AF_INET to AF_INET6 and changing the IP to my own IP address
  • Using INETPTON instead of inet_pton
  • redownloading mingw64

Might be relevant informations:

  • Running using VS Code C language
  • Using Windows 64bit, AMD Ryzen 7 7000 Series
  • MinGW32 from the official website
  • 2 friends using the exact same code, windows, vscode, mingw32/64 are both able to compile and execute client successfully while I couldn't

I am a student starting to learn socket programming using C, and when I am trying to create a client-server environment I am facing this error while compiling:

if (inet_pton(AF_INET6 , "10.163.3.30", &serv_addr.sin_addr) <= 0) {
         ^~~~~
warning: implicit declaration of function 'inet_pton' [-Wimplicit-function-declaration]
     if (inet_pton(AF_INET6 , "10.163.3.30", &serv_addr.sin_addr) <= 0) {
         ^~~~~
C:\Users\xtsub\AppData\Local\Temp\ccWTMEPx.o:client.c:(.text+0xe9): undefined reference to `inet_pton'

Below is my code for my client.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <winsock2.h>
#include <ws2tcpip.h>

#define PORT 8080  // Port number to connect to

int main() {
    WSADATA wsaData;
    SOCKET sock;
    struct sockaddr_in serv_addr;
    char buffer[1024] = {0};

    // Initialize Winsock
    if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
        printf("WSAStartup failed\n");
        return 1;
    }

    // 1. Create socket
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock == INVALID_SOCKET) {
        printf("Socket creation failed: %d\n", WSAGetLastError());
        WSACleanup();
        return 1;
    }

    // 2. Define server address
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(PORT);

    // Convert IPv4 address from text to binary form
    if (inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr) <= 0) {
        printf("Invalid address or address not supported\n");
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    // 3. Connect to the server
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("Connection to server failed: %d\n", WSAGetLastError());
        closesocket(sock);
        WSACleanup();
        return 1;
    }

    // 4. Send data to the server
    char *message = "Hello from client";
    send(sock, message, strlen(message), 0);
    printf("Sent to server: %s\n", message);

    // 5. Receive response from the server
    int valread = recv(sock, buffer, sizeof(buffer), 0);
    if (valread > 0) {
        buffer[valread] = '\0';  // Null-terminate the received string
        printf("Received from server: %s\n", buffer);
    } else {
        printf("No response from server or error occurred\n");
    }

    // 6. Close the socket
    closesocket(sock);

    // Cleanup Winsock
    WSACleanup();
    
    return 0;
}

I have another code for my server, server.c setup and it works correctly so I think maybe it is not a compiler error. (Compiled successfully and when prompted with ./server shows message "Server is listening on port 8080..."). I have search on this error and it seems the function is not recognized by the corresponding library header is in my MinGw folder. I asked a teaching assistant but unfortunately it is still not resolved for now. I will appreciate it if anyone knows the solution. I am not proficient at all in this so please bear with it if there are any errors/information lacking.

What I tried:

  • replacing AF_INET to AF_INET6 and changing the IP to my own IP address
  • Using INETPTON instead of inet_pton
  • redownloading mingw64

Might be relevant informations:

  • Running using VS Code C language
  • Using Windows 64bit, AMD Ryzen 7 7000 Series
  • MinGW32 from the official website
  • 2 friends using the exact same code, windows, vscode, mingw32/64 are both able to compile and execute client successfully while I couldn't
Share Improve this question asked Nov 21, 2024 at 18:49 Koh CwKoh Cw 174 bronze badges 3
  • 1 The "implicit declaration" warning tells you that none of the headers you're including provide a declaration of inet_pton(). This is a red flag, but not necessarily fatal. On the other hand, the "undefined reference" reference error tells you that none of the libraries included in the link provide such a function. Possibly relevant: github.com/msys2/MINGW-packages/issues/6191. – John Bollinger Commented Nov 21, 2024 at 19:19
  • inet_pton is defined in ws2tcpip.h only if you are compiling for Vista+. It is wrapped in an #if (NTDDI_VERSION >= NTDDI_VISTA) block. – Remy Lebeau Commented Nov 21, 2024 at 19:31
  • See stackoverflow.com/q/5619341/1216776 – stark Commented Nov 22, 2024 at 11:15
Add a comment  | 

1 Answer 1

Reset to default 1

[MS.Learn]: inet_pton function (ws2tcpip.h) (Requirements section) states (emphasis is mine):

Minimum supported client: Windows 8.1, Windows Vista [desktop apps | UWP apps]

[GitHub]: Does not support inet_pton on MinGW? (@zxgm's comment) (pointed out by @JohnBollinger's comment) mentions _WIN32_WINNT macro (more details: [MS.Learn]: Using the Windows Headers), being defined in _mingw.h (generated from e.g.: [GitHub]: mirrors/mingw-w64 - (v12.0.0) mingw-w64/mingw-w64-headers/crt/_mingw.h.in#231 - after the placeholder is replaced by its actual value (during build)):

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x502
#endif

corresponding to Windows Server 2003 which is too old.
All you have to do is adding the macro definition before including ws2tcpip.h:

#define _WIN32_WINNT _WIN32_WINNT_VISTA  // 0x0600

However, since _mingw.h is included by any header, this will yield a (macro redefinition) warning, that you can get rid of by moving the macro definition at the beginning of your program (before any includes).

Output (building your source file with the above change):

[cfati@CFATI-5510-0:e:\Work\Dev\StackExchange\StackOverflow\q079212570]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[prompt]> dir /b
main00.c

[prompt]> gcc --version | findstr gcc
'gcc' is not recognized as an internal or external command,
operable program or batch file.

[prompt]> set PATH=%PATH%;f:\Install\pc064\MinGW\MinGW-W64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin

[prompt]> gcc --version | findstr gcc
gcc (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

[prompt]> gcc -fPIC -o main.exe main00.c -lws2_32

[prompt]> dir /b
main.exe
main00.c

[prompt]>

本文标签: cImplicit declaration and undefined reference of function 39inetpton39Stack Overflow