为什么这个简单的 Qt 应用程序没有链接
我尝试编写一个简单的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用下面的代码,并确保在构建之前运行
qmake
(Build > Run qmake)。解释:当您包含
Q_OBJECT
宏时,这会指示 Qt 执行一系列非标准 C++ 的操作,例如信号和槽。它通过运行moc
来实现这一点,它在很大程度上是一个代码生成器。运行qmake
会创建元数据,以便在构建项目时,它知道要moc
哪些文件等。Use the code below, and make sure to run
qmake
(Build > Run qmake) before building.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 runningmoc
, which in large part is a code generator. Runningqmake
creates the metadata so that when your project is built, it knows which files tomoc
, etc.我认为您需要 moc 文件并将生成的 main.moc 包含在底部。
I think you need to moc the file and include the resulting main.moc at the bottom.
我认为这与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
我刚刚遇到了同样的问题,通过将标头的字符集从 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.