您的位置:首页 > 编程语言 > C语言/C++

moc_xxxx.cpp 和 xxxx.moc

2013-11-06 23:35 691 查看
用qmake处理moc文件:

qmake产生moc_xxxx.cpp并将其编译为可链接的moc_xxxx.o。在某些特定的情况下,可能比较好的用法是先产生moc_xxxx.cpp而不将其编译为*.o文件,然后moc_xxxx.cpp可以通过在xxxx.cpp中添加#include "moc_xxxx.cpp"方式被include。

stackoverflow上的说明:
http://stackoverflow.com/questions/5854626/qt-signals-and-slots-error-undefined-reference-to-vtable-for
Whenever you declare a QObject inside a filename.cpp file, you should add #include "filename.moc" to the end of your .cpp file and qmake will do the right thing for you. This is the common practice in KDE, for example.

关于moc_xxxx.cpp 和 xxxx.moc的产生:

when you declare a QObject class in a header file xxxx.h generated
file is moc_xxxx.cppwhen you declare a QObject class in a source file xxxx.cpp

generated file is xxxx.moc because
if you declare a QObject class in xxxx.h and another in xxxx.cpp the two generated files can not have the same name

它编译是是需要这个文件的,作为库提供给用户使用,不包含它的话用户工程中也会自动生成包含它,这不符封装规则,库的事情库自己应完成。如果库里使用了名字空间,更是这样,用户处理的话(实际上是qmake编译器帮完成的)就可能不知道名字空间从而产生问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: