inet_pton 443: error: 'inet_pton' was not declared in this scope
-
Hello everyone,
I am trying to use inet_pton in QT but it is giving error 'inet_pton' was not declared in this scope
I am working in windows10, QT version 5.14.2 with mingw73_64. I have added the library and header files as below
LIBS += -lws2_32 #include <winsock2.h> #include <winsock.h> #include <ws2tcpip.h>
and using function as
inet_pton(AF_INET, "192.168.6.166", destAddr);
I am getting this error 443: error: 'inet_pton' was not declared in this scope
inet_pton(AF_INET, "192.168.6.166", destAddr);Any help will be appreciated
Regards -
@Imran-Hassan said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
I am getting this error 443: error: 'inet_pton' was not declared in this scope
Is this the only error you get, or is there an error that ws2tcpip.h could not be found?
Are these includes in the same file where you call inet_pton?Also, according to https://docs.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-inet_pton it should be
#include <Ws2tcpip.h>
-
@jsulm said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
Is this the only error you get, or is there an error that ws2tcpip.h could not be found?
I am getting only this error. It is not giving any error related to the library and header file.
This is the full error I am getting
Full error is Sementic Issue 443:9 C:\Users\IMRANHASSAN\Documents\VideoCodecApp\mainwindow.cpp:443: error: 'inet_pton' was not declared in this scope inet_pton(AF_INET, "192.168.6.166", destAddr); ^~~~~~~~~ -
@Imran-Hassan You did not answer this question: "Are these includes in the same file where you call inet_pton?"
-
And please try to move the winapi includes before the Qt includes to see if it helps.
Also try to create a simple main.cpp where you only call inet_pton() to see if it works there. -
@jsulm said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
"Are these includes in the same file where you call inet_pton?"
Yes these are included in the same file. I checked it by both including in a header file and include that header file
Also just including the header files in same file both give the same error
-
@jsulm said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
Is this the only error you get, or is there an error that ws2tcpip.h could not be found?
Some time it also give this error
C:\Users\IMRANHASSAN\Documents\VideoCodecApp\mainwindow.cpp:450: error: use of undeclared identifier 'inet_pton'
-
@Christian-Ehrlicher said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
And please try to move the winapi includes before the Qt includes to see if it helps.
I tried this but same problem
Also try to create a simple main.cpp where you only call inet_pton() to see if it works there.
I made simple application
#include "mainwindow.h" #include "ui_mainwindow.h" //#include <winsock2.h> //#include <winsock.h> #include <ws2tcpip.h> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); PVOID* destAddr = new PVOID; // sockaddr_storage storage; inet_pton(AF_INET, "192.168.6.166", destAddr); } MainWindow::~MainWindow() { delete ui; }
.pro file is as
QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets CONFIG += c++17 LIBS += -lws2_32 # The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if it uses deprecated APIs. # In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ main.cpp \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
But the error is same
-
@Imran-Hassan You did not really do what @Christian-Ehrlicher suggested...
-
And why do you not use QtNetwork for it?
-
@jsulm said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
You did not really do what @Christian-Ehrlicher suggested...
I created a simple application with GUI and I also created simple console application. But in both I get same error
-
@Imran-Hassan said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
and I also created simple console application.
Please show the code
-
@Christian-Ehrlicher said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
Please show the code
Here is code
#include <ws2tcpip.h> #include <QCoreApplication> #include <QtNetwork> int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); PVOID* destAddr = new PVOID; // sockaddr_storage storage; inet_pton(AF_INET, "192.168.6.166", destAddr); return a.exec(); }
Pro file is
LIBS += -lws2_32 QT -= gui QT += network CONFIG += c++11 console CONFIG -= app_bundle DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ main.cpp # Default rules for deployment. qnx: target.path = /tmp/$${TARGET}/bin else: unix:!android: target.path = /opt/$${TARGET}/bin !isEmpty(target.path): INSTALLS += target
-
Ok, so according to the docs you must define _WIN32_WINNT to 0x0600 or higher:
Minimum supported client Windows 8.1, Windows Vista [desktop apps | UWP apps]
I still would suggest you to simply use QtNetwork for this instead some low-level api calls.
-
@Christian-Ehrlicher said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
according to the docs you must define _WIN32_WINNT to 0x0600 or higher
I have used #define _WIN32_WINNT_WIN10 0x0A00 // Windows 10
I still would suggest you to simply use QtNetwork for this instead some low-level api calls.
What is the alternative of inet_pton in QtNetwork?
-
@Imran-Hassan said in inet_pton 443: error: 'inet_pton' was not declared in this scope:
I have used #define _WIN32_WINNT_WIN10 0x0A00 // Windows 10
And what should this help? Please read my post
What is the alternative of inet_pton in QtNetwork?
"192.168.6.166" is a host address... https://doc.qt.io/qt-5/qhostaddress.html
-
@Christian-Ehrlicher Thanks dear. I will use this