无法解析的外部符号 C++

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了无法解析的外部符号 C++相关的知识,希望对你有一定的参考价值。

错误 3 error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const GlobeWidget::staticMetaObject" (?staticMetaObject@GlobeWidget@@2UQMetaObject@@B) E:\Platform\ProjectManagerControl\projectmanagercontrolplugin.obj ProjectManagerControl
错误 6 error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const FormLoadProject::staticMetaObject" (?staticMetaObject@FormLoadProject@@2UQMetaObject@@B) E:\Platform\ProjectManagerControl\projectmanagercontrolplugin.obj ProjectManagerControl
错误 9 error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const FormAddNewProject::staticMetaObject" (?staticMetaObject@FormAddNewProject@@2UQMetaObject@@B) E:\Platform\ProjectManagerControl\projectmanagercontrolplugin.obj ProjectManagerControl
错误 2 error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const GlobeWidget::staticMetaObject" (?staticMetaObject@GlobeWidget@@2UQMetaObject@@B) E:\Platform\ProjectManagerControl\projectmanagercontrol.obj ProjectManagerControl
错误 5 error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const FormLoadProject::staticMetaObject" (?staticMetaObject@FormLoadProject@@2UQMetaObject@@B) E:\Platform\ProjectManagerControl\projectmanagercontrol.obj ProjectManagerControl
错误 8 error LNK2001: 无法解析的外部符号 "public: static struct QMetaObject const FormAddNewProject::staticMetaObject" (?staticMetaObject@FormAddNewProject@@2UQMetaObject@@B) E:\Platform\ProjectManagerControl\projectmanagercontrol.obj ProjectManagerControl
错误 10 error LNK1120: 3 个无法解析的外部命令 E:\Platform\bin\Release\\ProjectManagerControl.dll ProjectManagerControl
错误 1 error LNK2019: 无法解析的外部符号 "public: static struct QMetaObject const GlobeWidget::staticMetaObject" (?staticMetaObject@GlobeWidget@@2UQMetaObject@@B),该符号在函数 "public: static class QString __cdecl GlobeWidget::tr(char const *,char const *)" (?tr@GlobeWidget@@SA?AVQString@@PBD0@Z) 中被引用 E:\Platform\ProjectManagerControl\moc_projectmanagercontrol.obj ProjectManagerControl
错误 4 error LNK2019: 无法解析的外部符号 "public: static struct QMetaObject const FormLoadProject::staticMetaObject" (?staticMetaObject@FormLoadProject@@2UQMetaObject@@B),该符号在函数 "public: static class QString __cdecl FormLoadProject::tr(char const *,char const *)" (?tr@FormLoadProject@@SA?AVQString@@PBD0@Z) 中被引用 E:\Platform\ProjectManagerControl\moc_projectmanagercontrol.obj ProjectManagerControl
错误 7 error LNK2019: 无法解析的外部符号 "public: static struct QMetaObject const FormAddNewProject::staticMetaObject" (?staticMetaObject@FormAddNewProject@@2UQMetaObject@@B),该符号在函数 "public: static class QString __cdecl FormAddNewProject::tr(char const *,char const *)" (?tr@FormAddNewProject@@SA?AVQString@@PBD0@Z) 中被引用 E:\Platform\ProjectManagerControl\moc_projectmanagercontrol.obj ProjectManagerControl

参考技术A lz是在编译qt相关的代码是吧?
出现这种问题,一般是你缺少了部分库(.lib)的链接过程。请检查下工程的设置。看看有哪些依赖项目需要手动添加。
因为lz没有提供足够多的细节(使用什么IDE,用什么编译器,qt版本如何,什么样的工程等等)。只能给出上面的大概解决方案。详细请lz按照上面所说的方向自己再研究。本回答被提问者和网友采纳

Qt C++ LNK2019:使用 QNetworkAccessManager 无法解析的外部符号

【中文标题】Qt C++ LNK2019:使用 QNetworkAccessManager 无法解析的外部符号【英文标题】:Qt C++ LNK2019: unresolved external symbol using QNetworkAccessManager 【发布时间】:2019-11-10 07:45:27 【问题描述】:

我正在尝试使用https://doc.qt.io/qt-5/qnetworkaccessmanager.html 此处提供的文档运行一段简单的代码。我在 Windows 10 上使用带有 MSVC2017 64 位工具包的 Qt Creator。我对 C++ 和类的了解非常基础。

代码给出了我不知道为什么的链接器错误。我曾尝试再次清洁和重建,但没有成功。任何帮助,将不胜感激。谢谢!

我在 Qt Creator 中使用 mainwindow.h 创建了一个新项目

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>

QT_BEGIN_NAMESPACE
namespace Ui  class MainWindow; 
QT_END_NAMESPACE

class MainWindow : public QMainWindow

    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);    
    ~MainWindow();
    void replyFinished(QNetworkReply *reply);

private:
    Ui::MainWindow *ui;    
;
#endif // MAINWINDOW_H

然后在mainwindow.cpp中添加这些行并编译。

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)

    ui->setupUi(this);
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, &QNetworkAccessManager::finished,
            this, &MainWindow::replyFinished);

    manager->get(QNetworkRequest(QUrl("http://qt-project.org")));


void MainWindow::replyFinished(QNetworkReply *reply)

    //

当我使用 MSVC2017 64 位套件编译时,我在输出中遇到链接器错误。如果我使用不同的套件 (MinGW),问题也是一样的。

这是我的 .pro 文件:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# 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


这是编译器的输出:

12:52:23: Running steps for project Proj2...
12:52:23: Configuration unchanged, skipping qmake step.
12:52:23: Starting: "C:\Qt\Tools\QtCreator\bin\jom.exe" 
    C:\Qt\Tools\QtCreator\bin\jom.exe -f Makefile.Debug
    link /NOLOGO /DYNAMICBASE /NXCOMPAT /DEBUG /SUBSYSTEM:WINDOWS "/MANIFESTDEPENDENCY:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' publicKeyToken='6595b64144ccf1df' language='*' processorArchitecture='*'" /MANIFEST:embed /OUT:debug\Proj2.exe @C:\Users\HP\AppData\Local\Temp\Proj2.exe.7376.15.jom
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QNetworkRequest::QNetworkRequest(class QUrl const &)" (__imp_??0QNetworkRequest@@QEAA@AEBVQUrl@@@Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QNetworkRequest::~QNetworkRequest(void)" (__imp_??1QNetworkRequest@@QEAA@XZ) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QNetworkAccessManager::QNetworkAccessManager(class QObject *)" (__imp_??0QNetworkAccessManager@@QEAA@PEAVQObject@@@Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __cdecl QNetworkAccessManager::~QNetworkAccessManager(void)" (__imp_??1QNetworkAccessManager@@UEAA@XZ) referenced in function "public: virtual void * __cdecl QNetworkAccessManager::`scalar deleting destructor'(unsigned int)" (??_GQNetworkAccessManager@@UEAAPEAXI@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: class QNetworkReply * __cdecl QNetworkAccessManager::get(class QNetworkRequest const &)" (__imp_?get@QNetworkAccessManager@@QEAAPEAVQNetworkReply@@AEBVQNetworkRequest@@@Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
mainwindow.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __cdecl QNetworkAccessManager::finished(class QNetworkReply *)" (__imp_?finished@QNetworkAccessManager@@QEAAXPEAVQNetworkReply@@@Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QEAA@PEAVQWidget@@@Z)
mainwindow.obj : error LNK2001: unresolved external symbol "protected: virtual class QNetworkReply * __cdecl QNetworkAccessManager::createRequest(enum QNetworkAccessManager::Operation,class QNetworkRequest const &,class QIODevice *)" (?createRequest@QNetworkAccessManager@@MEAAPEAVQNetworkReply@@W4Operation@1@AEBVQNetworkRequest@@PEAVQIODevice@@@Z)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl QNetworkAccessManager::metaObject(void)const " (?metaObject@QNetworkAccessManager@@UEBAPEBUQMetaObject@@XZ)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual int __cdecl QNetworkAccessManager::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@QNetworkAccessManager@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)
mainwindow.obj : error LNK2001: unresolved external symbol "public: virtual void * __cdecl QNetworkAccessManager::qt_metacast(char const *)" (?qt_metacast@QNetworkAccessManager@@UEAAPEAXPEBD@Z)
mainwindow.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const QNetworkAccessManager::staticMetaObject" (__imp_?staticMetaObject@QNetworkAccessManager@@2UQMetaObject@@B)
  Hint on symbols that are defined and could potentially match:
    "__declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject" (__imp_?staticMetaObject@QMainWindow@@2UQMetaObject@@B)
mainwindow.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static struct QMetaObject const QNetworkReply::staticMetaObject" (__imp_?staticMetaObject@QNetworkReply@@2UQMetaObject@@B)
  Hint on symbols that are defined and could potentially match:
    "__declspec(dllimport) public: static struct QMetaObject const QMainWindow::staticMetaObject" (__imp_?staticMetaObject@QMainWindow@@2UQMetaObject@@B)
debug\Proj2.exe : fatal error LNK1120: 12 unresolved externals

【问题讨论】:

@hyde 感谢您查看此内容。我已经更新了问题。 【参考方案1】:

您的 .pro 文件缺少网络模块导入。更改第一行以添加网络库。

QT += core gui network

你可以从the documentation看到这个,上面写着

qmake: QT += 网络

【讨论】:

以上是关于无法解析的外部符号 C++的主要内容,如果未能解决你的问题,请参考以下文章

C++无法解析的外部符号

C++ error LNK2001: 无法解析的外部符号

c++ 无法解析的外部符号 1>p.obj : error LNK2001: 无法解析的外部符号 "public: virtual void __thiscall

C++学习(四五五)error: LNK2019: 无法解析的外部符号

在 Windows 上构建 C++ 项目时,Tensorflow 2.3 无法解析机器生成的文件中的外部符号

“error LNK2019: 无法解析的外部符号”之分析