为什么这个简单的 Qt 应用程序没有链接

发布于 2024-12-17 17:01:56 字数 1262 浏览 2 评论 0原文

我尝试编写一个简单的 Qt 应用程序,如下所示:

main.cpp

#include <QApplication>

class MyApp : public QApplication {
        Q_OBJECT
public:
        MyApp(int argc, char* argv[]);
};

MyApp::MyApp(int argc, char* argv[]) :
        QApplication(argc,argv) {
}

int main(int argc, char* argv[]) {
    MyApp app(argc,argv);
    return app.exec();
}

但是当我尝试使用 Qt Creator 2.3.1 (Qt 4.7.4) 编译并链接它时,我得到 3“未解析的外部符号" 错误:

  • <块引用>

    main.obj:-1: 错误: LNK2001:无法解析的外部符号
    ""public: virtual struct QMetaObject const * __thiscall MyApp::metaObject(void)const "
    (?metaObject@MyApp@@UBEPBUQMetaObject@@XZ)”。

  • main.obj:-1: 错误: LNK2001:无法解析的外部符号
    ""public: virtual void * __thiscall MyApp::qt_metacast(char const*)"
    (?qt_metacast@MyApp@@UAEPAXPBD@Z)”。

  • <块引用>

    main.obj:-1: 错误: LNK2001:无法解析的外部符号
    ""public: virtual int __thiscall MyApp::qt_metacall(enum QMetaObject::Call,int,void * *)"
    (?qt_metacall@MyApp@@UAEHW4Call@QMetaObject@@HPAPAX@Z)"。

我认为它们与Qt的MetaObjectCompiler有某种关系,但我找不到解决方案。 我知道在 C++ 中将声明和定义放在一个文件中并不被认为是良好的编程风格,但这不是重点。在我看来,这应该是可能的,因为这里没有任何语法错误。

I tried to write a simple Qt application like this:

main.cpp:

#include <QApplication>

class MyApp : public QApplication {
        Q_OBJECT
public:
        MyApp(int argc, char* argv[]);
};

MyApp::MyApp(int argc, char* argv[]) :
        QApplication(argc,argv) {
}

int main(int argc, char* argv[]) {
    MyApp app(argc,argv);
    return app.exec();
}

But when I tried to compile and link it with Qt Creator 2.3.1 (Qt 4.7.4) I get 3 "unresolved external symbol" errors:

  • main.obj:-1: error:
    LNK2001: unresolved external symbol
    ""public: virtual struct QMetaObject const * __thiscall MyApp::metaObject(void)const "
    (?metaObject@MyApp@@UBEPBUQMetaObject@@XZ)".

  • main.obj:-1: error:
    LNK2001: unresolved external symbol
    ""public: virtual void * __thiscall MyApp::qt_metacast(char const*)"
    (?qt_metacast@MyApp@@UAEPAXPBD@Z)".

  • main.obj:-1: error:
    LNK2001: unresolved external symbol
    ""public: virtual int __thiscall MyApp::qt_metacall(enum QMetaObject::Call,int,void * *)"
    (?qt_metacall@MyApp@@UAEHW4Call@QMetaObject@@HPAPAX@Z)".

I think they are somehow related to the MetaObjectCompiler of Qt, but I can't figure out a solution.
I know it's not considered good programming style in c++ to put declarations and definitions in one file, but that's not the point here. In my opinion it should be possible since there is nothing syntactically wrong here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

当爱已成负担 2024-12-24 17:01:56

使用下面的代码,并确保在构建之前运行qmake(Build > Run qmake)。

#include <QApplication>

class MyApp : public QApplication {
  Q_OBJECT
public:
  MyApp(int argc, char* argv[]);
};

MyApp::MyApp(int argc, char* argv[]) :
  QApplication(argc,argv) {
}

int main(int argc, char* argv[]) {
  MyApp app(argc,argv);
  return app.exec();
}

#include "main.moc"

解释:当您包含 Q_OBJECT 宏时,这会指示 Qt 执行一系列非标准 C++ 的操作,例如信号和槽。它通过运行 moc 来实现这一点,它在很大程度上是一个代码生成器。运行 qmake 会创建元数据,以便在构建项目时,它知道要 moc 哪些文件等。

Use the code below, and make sure to run qmake (Build > Run qmake) before building.

#include <QApplication>

class MyApp : public QApplication {
  Q_OBJECT
public:
  MyApp(int argc, char* argv[]);
};

MyApp::MyApp(int argc, char* argv[]) :
  QApplication(argc,argv) {
}

int main(int argc, char* argv[]) {
  MyApp app(argc,argv);
  return app.exec();
}

#include "main.moc"

Explanation: When you include the Q_OBJECT macro, this signals Qt to do a bunch of stuff that is not standard C++, such as signals and slots. It does this by running moc, which in large part is a code generator. Running qmake creates the metadata so that when your project is built, it knows which files to moc, etc.

沐歌 2024-12-24 17:01:56

我认为您需要 moc 文件并将生成的 main.moc 包含在底部。

I think you need to moc the file and include the resulting main.moc at the bottom.

沐歌 2024-12-24 17:01:56

我认为这与QMake有关。这并不是说可执行应用程序看不到导出的 DLL 类。这是因为该类的 obj 文件不存在。从 QT Creator Build 菜单运行 QMake,然后构建似乎可以工作。

为什么这个简单的 Qt 应用程序不链接

I think this has something to do with QMake. It's not that the executable app can't see the exported DLL class. It's that the obj file for the class doesn't exist. Running QMake from the QT Creator Build menu and then building seems to work.

Why does this Simple Qt Application not link

筑梦 2024-12-24 17:01:56

我刚刚遇到了同样的问题,通过将标头的字符集从 Unicode 更改为 ANSI 已解决。

I've just met the same problem, and it has been solved by changing the Character set of my header from Unicode to ANSI.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文