search

Home  >  Q&A  >  body text

How can a C++ program written in vs2017 be copied to Qt Creator without reporting an error? Please let me know.

This is a piece of code I found on the Internet. It is no problem to put it in vs2017. Most header files cannot be used in Qt, and some keywords cannot be used in Qt. Errors and warnings will be reported. Qt can be programmed under window. How to solve this problem

#include <iostream>
#include "winsock2.h"
#pragma comment(lib,"ws2_32.lib")
using namespace std;
int main(int argc, char *argv[])
{
    const int BUF_SIZE = 64;
    WSADATA wsd;//WSADATA变量
    SOCKET sServer;//服务端套接字
    SOCKET sClient;//客户端套接字
    SOCKADDR_IN addrServ;//服务器地址
    char buf[BUF_SIZE];//接受数据缓冲区
    char sendBuf[BUF_SIZE];//返回给客户端的数据
    int retVal;//返回值
                //初始化套接字动态库
    if(WSAStartup(MAKEWORD(2,2),&wsd) != 0)
    {
        cout << "WSAStartup failed!" << endl;
        return 1;
    }
    //创建套接字
    sServer = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(INVALID_SOCKET == sServer)
    {
        cout << "socket failed!" << endl;
        WSACleanup();//释放套接字资源
        return -1;
    }
    //服务器套接字地址
    addrServ.sin_family = AF_INET;
    addrServ.sin_port = htons(6666);
    addrServ.sin_addr.s_addr = INADDR_ANY;
    //绑定套接字
    retVal = bind(sServer,(LPSOCKADDR)&addrServ,sizeof(SOCKADDR_IN));
    if(SOCKET_ERROR == retVal)
    {
        cout << "bind failed!" << endl;
        closesocket(sServer);//关闭套接字
        WSACleanup();//释放套接字资源
        return -1;
    }
    //开始监听
    retVal = listen(sServer,1);
    if(SOCKET_ERROR == retVal)
    {
        cout << "listen failed!" << endl;
        closesocket(sServer);//关闭套接字
        WSACleanup();//释放套接字资源
        return -1;
    }
    //接受客户端请求
    sockaddr_in addrClient;
    int addrClientlen = sizeof(addrClient);
    sClient = accept(sServer,(sockaddr FAR*)&addrClient,&addrClientlen);
    if(INVALID_SOCKET == sClient)
    {
        cout << "accept failed!" << endl;
        closesocket(sServer);//关闭套接字
        WSACleanup();//释放套接字资源
        return -1;
    }
    while(true)
    {
        //接受客户端数据
        ZeroMemory(buf,BUF_SIZE);
        retVal = recv(sClient,buf,BUF_SIZE,0);
        if(SOCKET_ERROR == retVal)
        {
            cout << "recv failed!" << endl;
            closesocket(sServer);//关闭套接字
            WSACleanup();//释放套接字资源
            return -1;
        }
        if(buf[0] == '0')
        {
            break;
        }
        cout << "客户端发送的数据:" << buf << endl;
        cout << "向客户端发送数据:";
        cin >> sendBuf;
        send(sClient,sendBuf,strlen(sendBuf),0);
    }
    //退出
    closesocket(sServer);//关闭套接字
    closesocket(sClient);//关闭套接字
    WSACleanup();//释放套接字资源
    return 0;
}

Error message:
C:UsersAdministrationDocumentsQtservermain.cpp:4: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(lib,"ws2_32.lib")
C:UsersAdministrationDocumentsQtservermain .cpp:18: error: undefined reference to `_imp__WSAStartup@8'
C:UsersAdministrationDocumentsQtservermain.cpp:24: error: undefined reference to `_imp__socket@12'
C:UsersAdministrationDocumentsQtservermain.cpp:28: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:33: error: undefined reference to `_imp__htons@4'
C:UsersAdministrationDocumentsQtservermain.cpp:36: error: undefined reference to `_imp__bind@12'
C:UsersAdministrationDocumentsQtservermain.cpp:40: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:41: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp: 45: error: undefined reference to `_imp__listen@8'
C:UsersAdministrationDocumentsQtservermain.cpp:49: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:50: error: undefined reference to ` _imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:56: error: undefined reference to `_imp__accept@12'
C:UsersAdministrationDocumentsQtservermain.cpp:60: error: undefined reference to `_imp__closesocket@4'
C :UsersAdministrationDocumentsQtservermain.cpp:61: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:68: error: undefined reference to `_imp__recv@16'
C:UsersAdministrationDocumentsQtservermain.cpp:72: error : undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:73: error: undefined reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:83: error: undefined reference to `_imp__send@16 '
C:UsersAdministrationDocumentsQtservermain.cpp:86: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:87: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain. cpp:88: error: undefined reference to `_imp__WSACleanup@0'
collect2.exe:-1: error: error: ld returned 1 exit status
There are header file errors and keyword errors

高洛峰高洛峰2743 days ago1471

reply all(1)I'll reply

  • 大家讲道理

    大家讲道理2017-05-31 10:42:01

    winsock?
    Didn’t it give a warning?
    warning: ignoring #pragma comment [-Wunknown-pragmas]
    Link problem.
    What is your qt compiler configuration? mscv?

    If you choose msvc, you can try it in .pro

    win32:LIBS += xxx.lib
    win32:QMAKE_POST_LINK += xxx.dll

    PS: If qt has a network library, you can use qt’s network library through qt +=network.

    reply
    0
  • Cancelreply