vs2017所寫的C++程式如何能複製到Qt Creator上而不報錯,求大神告知
高洛峰
高洛峰 2017-05-31 10:40:01
0
1
1463

這是我在網路上找的一段程式碼,放在vs2017裡面沒問題。大多數是頭檔在qt裡面不能用,有的關鍵字在qt裡面也不能用,會報錯和警告,qt不是可以在window下面編程的嘛,遇到這種問題該怎麼解決

#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;
}

錯誤提示:
C:UsersAdministrationDocumentsQtservermain.cpp:4: warning: ignoring #pragma comment [-Wunknown-pragmas]
#pragma comment(lib,"ws2_32.lib")##rationC:UsersAdministrationDocuments .cpp:18: error: undefined reference to `_imp__WSAStartup@8'
C:UsersAdministrationDocumentsQtservermain.cpp:24: error: undefined reference to `_imp__socket@12'fidad:12'>Admin.S.A.D.Dem.DatLidem:Doud:U​​id-wem. reference to `_imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:33: error: undefined reference to `_imp__htons@4'
C:UsersAdministrationDocumentsQtservermain.cerrpp:36:
C:UsersAdministrationDocumentsQtservermain.cerrpp:36: d##d. ##C:UsersAdministrationDocumentsQtservermain.cpp:40: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:41: error: undefined reference 至 00118L -41: 45: error: undefined reference to `_imp__listen@8'
C:UsersAdministrationDocumentsQtservermain.cpp:49: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministration defunments. _imp__WSACleanup@0'
C:UsersAdministrationDocumentsQtservermain.cpp:56: error: undefined reference to `_imp__accept@12'
C:UsersAdministrationDocumentsQtservermain.cpp:6012'
C:UsersAdministrationDocumentsQtservermain.cpp:602'
C:UsersAdministrationDocumentsQtservermain.cpp:602'#det: 6006 月。 :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:UsersAdministrationDocumentst.13. '
C:UsersAdministrationDocumentsQtservermain.cpp:86: error: undefined reference to `_imp__closesocket@4'
C:UsersAdministrationDocumentsQtservermain.cpp:87: error: undefined reference cpp:88: error: undefined reference to `_imp__WSACleanup@0'
collect2.exe:-1: error: error: ld returned 1 exit status
有頭檔的錯誤,也有關鍵字的錯誤

高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

全部回覆(1)
大家讲道理

winsock?
不是給了警告麼?
warning: ignoring #pragma comment [-Wunknown-pragmas]
鏈接問題.
你的qt的編譯器配置的什麼? mscv?

鏈接問題.

你的qt的編譯器配置的什麼? mscv?

如果選的是msvc 可以試試在.pro

win32:LIBS += xxx.lib
win32:QMAKE_POST_LINK += xxx.dll
PS: qt有網路庫的,可以透過

qt +=network🎜使用qt的網路庫.🎜
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板