2

我在 QtCreator (QT 4.8.2) 中创建了一个简单的 GUI 应用程序,并添加了一个检查网络连接的功能:

bool MainWindow::CheckNetworkConnectivity()
{
    QList<QNetworkInterface> interface = QNetworkInterface::allInterfaces();
    for(int i = 0; i < interface.count();i++)
    {
        QNetworkInterface inface = interface.at(i);
        if(inface.IsUp && !inface.IsLoopBack)
        {
            ui->lstLog->addItem(inface.name()+QDateTime::currentDateTime().toString("h:m:s ap"));
            ui->chkConStatus->setChecked(true);
        }
        else{
            ui->chkConStatus->setChecked(false);
        }
    }
    return  ui->chkConStatus->checkState();
}

之后,任何编译代码的尝试都会产生以下错误:

    15:21:11: Running steps for project MasterVPN...
15:21:11: Configuration unchanged, skipping qmake step.
15:21:11: Starting: "C:\Qt\Designer\bin\jom.exe" 
    C:\Qt\Designer\bin\jom.exe -f Makefile.Debug
    cl -c -nologo -Zm200 -Zc:wchar_t- -Zi -MDd -GR -EHsc -W3 -w34100 -w34189 -DUNICODE -DWIN32 -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -I"c:\Qt\4.8.2\include\QtCore" -I"c:\Qt\4.8.2\include\QtGui" -I"c:\Qt\4.8.2\include" -I"c:\Qt\4.8.2\include\ActiveQt" -I"debug" -I"." -I"..\MasterVPN" -I"." -I"c:\Qt\4.8.2\mkspecs\win32-msvc2010" -Fodebug\ @C:\Users\Master\AppData\Local\Temp\mainwindow.obj.4156.31.jom
mainwindow.cpp
    link /LIBPATH:"c:\Qt\4.8.2\lib" /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /MANIFEST /MANIFESTFILE:"debug\MasterVPN.intermediate.manifest" /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /OUT:debug\MasterVPN.exe @C:\Users\Master\AppData\Local\Temp\MasterVPN.exe.4156.5422.jom
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QNetworkInterface::~QNetworkInterface(void)" (__imp_??1QNetworkInterface@@QAE@XZ) referenced in function "public: bool __thiscall MainWindow::CheckNetworkConnectivity(void)" (?CheckNetworkConnectivity@MainWindow@@QAE_NXZ)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QString __thiscall QNetworkInterface::name(void)const " (__imp_?name@QNetworkInterface@@QBE?AVQString@@XZ) referenced in function "public: bool __thiscall MainWindow::CheckNetworkConnectivity(void)" (?CheckNetworkConnectivity@MainWindow@@QAE_NXZ)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall QNetworkInterface::QNetworkInterface(class QNetworkInterface const &)" (__imp_??0QNetworkInterface@@QAE@ABV0@@Z) referenced in function "public: bool __thiscall MainWindow::CheckNetworkConnectivity(void)" (?CheckNetworkConnectivity@MainWindow@@QAE_NXZ)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class QList<class QNetworkInterface> __cdecl QNetworkInterface::allInterfaces(void)" (__imp_?allInterfaces@QNetworkInterface@@SA?AV?$QList@VQNetworkInterface@@@@XZ) referenced in function "public: bool __thiscall MainWindow::CheckNetworkConnectivity(void)" (?CheckNetworkConnectivity@MainWindow@@QAE_NXZ)
debug\MasterVPN.exe : fatal error LNK1120: 4 unresolved externals
jom: C:\Users\Master\Documents\Qt\MasterVPN-build-desktop-Qt_4_8_2__4_8_2__Debug\Makefile.Debug [debug\MasterVPN.exe] Error 1120
jom: C:\Users\Master\Documents\Qt\MasterVPN-build-desktop-Qt_4_8_2__4_8_2__Debug\Makefile [debug] Error 2
15:21:17: The process "C:\Qt\Designer\bin\jom.exe" exited with code 2.
Error while building/deploying project MasterVPN (target: Desktop)
When executing step 'Make'

有人可以指出可能是什么原因吗?

4

1 回答 1

6

链接器指示找不到所需的功能。这包含在 QtNetwork4.lib(如果您在 Windows 平台上)或您平台的等效库中。所以你必须告诉链接器它需要链接到这个库。网络模块是必需的,所以不仅仅是

QT += core gui

至少做到这一点

QT += core gui network

这应该可以解决您看到的链接器错误。

于 2012-09-03T12:04:30.090 回答