Qt 添加 QtNetwork 库文件(包括vs中和qt中)
Qt应用程序默认没有加QtNetwork库。如下图:
在开发过程中,因处理业务需要手动添加QtNetwork库。根据常见情况分为以下两种:
【1】若使用QTCreator开发程序
在工程的pro文件中添加:QT += network
【2】若使用VS + QT开发程序
本地环境VS2017 + QT5.9.2 编译错误提示如下:
1 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl QNetworkRequest::QNetworkRequest(class QUrl const &)" ([email protected]@[email protected]@@@Z),该符号在函数 "public: void __cdecl UPCLoginService::execute(void)" ([email protected]@@QEAAXXZ) 中被引用 2 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl QNetworkRequest::~QNetworkRequest(void)" ([email protected]@[email protected]),该符号在函数 "public: void __cdecl UPCLoginService::execute(void)" ([email protected]@@QEAAXXZ) 中被引用 3 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __cdecl QNetworkRequest::setUrl(class QUrl const &)" ([email protected]@@[email protected]@@Z),该符号在函数 "public: void __cdecl UPCLoginService::execute(void)" ([email protected]@@QEAAXXZ) 中被引用 4 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __cdecl QNetworkRequest::setHeader(enum QNetworkRequest::KnownHeaders,class QVariant const &)" ([email protected]@@[email protected]@[email protected]@@Z),该符号在函数 "public: void __cdecl UPCLoginService::execute(void)" ([email protected]@@QEAAXXZ) 中被引用 5 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __cdecl QNetworkRequest::setSslConfiguration(class QSslConfiguration const &)" ([email protected]@@[email protected]@@Z),该符号在函数 "public: void __cdecl UPCLoginService::execute(void)" ([email protected]@@QEAAXXZ) 中被引用 6 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl QSslConfiguration::QSslConfiguration(void)" ([email protected]@[email protected]),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 7 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl QSslConfiguration::~QSslConfiguration(void)" ([email protected]@[email protected]),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 8 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: class QSslConfiguration & __cdecl QSslConfiguration::operator=(class QSslConfiguration &&)" ([email protected]@[email protected][email protected]@Z),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 9 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __cdecl QSslConfiguration::setProtocol(enum QSsl::SslProtocol)" ([email protected]@@[email protected]@@@Z),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 10 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __cdecl QSslConfiguration::setPeerVerifyMode(enum QSslSocket::PeerVerifyMode)" ([email protected]@@[email protected]@@@Z),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 11 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: static class QSslConfiguration __cdecl QSslConfiguration::defaultConfiguration(void)" ([email protected]@@[email protected]),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 12 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __cdecl QNetworkAccessManager::QNetworkAccessManager(class QObject *)" ([email protected]@[email protected]@@@Z),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 13 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual __cdecl QNetworkAccessManager::~QNetworkAccessManager(void)" ([email protected]@[email protected]),该符号在函数 "public: virtual __cdecl UPCLoginService::~UPCLoginService(void)" ([email protected]@[email protected]) 中被引用 14 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: class QNetworkReply * __cdecl QNetworkAccessManager::post(class QNetworkRequest const &,class QByteArray const &)" ([email protected]@@[email protected]@[email protected]@[email protected]@@Z),该符号在函数 "public: void __cdecl UPCLoginService::execute(void)" ([email protected]@@QEAAXXZ) 中被引用 15 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: void __cdecl QNetworkAccessManager::finished(class QNetworkReply *)" ([email protected]@@[email protected]@@Z),该符号在函数 "public: __cdecl UPCLoginService::UPCLoginService(class QObject *)" ([email protected]@[email protected]@@@Z) 中被引用 16 error LNK2001: 无法解析的外部符号 "protected: virtual class QNetworkReply * __cdecl QNetworkAccessManager::createRequest(enum QNetworkAccessManager::Operation,class QNetworkRequest const &,class QIODevice *)" ([email protected]@@[email protected]@[email protected]@[email protected]@[email protected]@@Z) 17 error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: static struct QMetaObject const QNetworkAccessManager::staticMetaObject" ([email protected]@@[email protected]@B) 18 error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: static struct QMetaObject const QNetworkAccessManager::staticMetaObject" ([email protected]@@[email protected]@B) 19 error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: static struct QMetaObject const QNetworkReply::staticMetaObject" ([email protected]@@[email protected]@B) 20 error LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: static struct QMetaObject const QNetworkReply::staticMetaObject" ([email protected]@@[email protected]@B) 21 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual void * __cdecl QNetworkAccessManager::qt_metacast(char const *)" ([email protected]@@[email protected]),该符号在函数 "public: virtual void * __cdecl UPCLoginService::qt_metacast(char const *)" ([email protected]@@[email protected]) 中被引用 22 error LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: virtual int __cdecl QNetworkAccessManager::qt_metacall(enum QMetaObject::Call,int,void * *)" ([email protected]@@[email protected]@@[email protected]),该符号在函数 "public: virtual int __cdecl UPCLoginService::qt_metacall(enum QMetaObject::Call,int,void * *)" ([email protected]@@[email protected]@@[email protected]) 中被引用
一般出现此链接问题都是没有添加库文件造成的。
很明显,程序中使用了QtNetwork库,那么就需要手动添加QtNetwork库文件:
(1)在debug模式,需要加Q5tNetworkd.lib库文件
(2)在release模式,需要加Qt5Network.lib库文件
当使用VS进行开发时在哪里添加呢?共有两个地方需要添加(缺一不可):
(1)项目->属性->c/c++->常规->附加包含目录->在弹出的对话框中,点那个文件夹形状的按钮添加新行,输入$(QTDIR)\include\QtNetwork
具体如图所示:
(2)项目->属性->连接器->输入->附加依赖项,添加 Qt5Networkd.lib(debug模式)或者 Qt5Network.lib(release模式)
具体如图所示:
添加完成后,再编译。若仍然报错,请仔细复盘如上步骤。
备注:其他模块库的添加类比如上步骤。