Unresolved externals and some warnings while building the QT solution
-
I recently started learning QT (with the help of book Foundations of Qt Development) and I have Qt libraries 4.8.1 for Windows (VS 2010 ultimate) with Qt Visual Studio Add-in. This is my very simple app :
@#include<qobject.h>
#include<qstring.h>
#include<memory>class MyClass : public QObject{
Q_OBJECTpublic:
MyClass( const QString &text, QObject *parent = 0 ) : m_text(text) {}public slots:
void setText( const QString &text );signals:
void textChanged( const QString& );private:
QString m_text;
};void MyClass::setText( const QString &text ){
if( m_text == text ) return;
m_text = text;
emit textChanged( m_text );
}int main(int argc, char *argv[]){
std::shared_ptr<MyClass> a(new MyClass("foo"));
std::shared_ptr<MyClass> b(new MyClass("bar"));QObject::connect( a.get(), SIGNAL(textChanged(const QString&)),
b.get(), SLOT(setText(const QString&)) );
a->setText("changed");
}@Errors I get related to unresolved externals :
bq. Error 6 error LNK2001: unresolved external symbol "public: virtual int __thiscall MyClass::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MyClass@@UAEHW4Call@QMetaObject@@HPAPAX@Z)
bq. Error 4 error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MyClass::metaObject(void)const " (?metaObject@MyClass@@UBEPBUQMetaObject@@XZ)
bq. Error 5 error LNK2001: unresolved external symbol "public: virtual void * __thiscall MyClass::qt_metacast(char const *)" (?qt_metacast@MyClass@@UAEPAXPBD@Z)
bq. Error 3 error LNK2019: unresolved external symbol "protected: void __thiscall MyClass::textChanged(class QString const &)" (?textChanged@MyClass@@IAEXABVQString@@@Z) referenced in function "public: void __thiscall MyClass::setText(class QString const &)" (?setText@MyClass@@QAEXABVQString@@@Z)
and two warnings :
bq. Warning 1 warning MSB8017: A circular dependency has been detected while executing custom build commands for item "GeneratedFiles\Debug\main.moc". This may cause incremental build to work incorrectly.
bq. Warning 2 warning : No resources in 'C:\Users\Anonymous\documents\visual studio 2010\Projects\qtWorld\qtWorld\qtworld.qrc'.
I didn't use any qmake / nmake . They ain't required when you get latest Qt Visual Studio Add-in 1.1.11 (even Intellisense recognizes the keywords slots: signals:), right?
Now my questions :
- I heard many errors are resolved by just rebuilding the whole solution , why is that ?
- Please explain why I get these errors in deep and their possible solutions.
-
If you have QObject class defined in cpp file then you should also include moc for that file.
For example your MyClass is defined in main.cpp then you should add
@
#include "main.moc"
@to end of your main.cpp file.
-
bq. "If you have QObject class defined in cpp file then you should also include moc for that file."
what that means ?, please use beginners word? (I'm very unfamiliar to moc/qmake/namke etc)
also why I need to include that main.moc file at the end of main.cpp file?. Though what you said has made my solution build successfully but I don't see any console window 0oYou still didn't reply to my bolded text question at bottom of question and last two questions following it , so please give them a look?
I really appreciate your help, Thanks
-
myclass.h
@#include<qobject.h>
#include<qstring.h>
#include<memory>class MyClass : public QObject{
Q_OBJECTpublic:
MyClass( const QString &text, QObject *parent = 0 ) : m_text(text) {}
public slots:
void setText( const QString &text );signals:
void textChanged( const QString& );private:
QString m_text;
};
@main.cpp
@
#include "myclass.h"void MyClass::setText( const QString &text ){
if( m_text == text ) return;
m_text = text;
emit textChanged( m_text );
}int main(int argc, char *argv[]){
std::shared_ptr<MyClass> a(new MyClass("foo"));
std::shared_ptr<MyClass> b(new MyClass("bar"));QObject::connect( a.get(), SIGNAL(textChanged(const QString&)),
b.get(), SLOT(setText(const QString&)) );
a->setText("changed");
}@As explained in "this answer":http://qt-project.org/forums/viewthread/19808/#95845 you need to start out with a Qt project.
However, the moc is typically applied to header files only. Since your class definition is part of the main program file, the IDE does not recognize it. I have separated the files. Try it this way. I did not try to compile. Let's see if there is still the linking problem.
-
@koahnig I really appreciate the answer , that explains why I was seeing that behavior , now everything works fine. though I don't see console printing anything
Edit : seems if I create qt console app , it does shows the console . Thanks koahnig
-
That is different between linux and windows. Under linux you see also console for gui applications. Under windows (at least with vc as an ide) the console is not seen. As you noticed is is important the select the correct application. However, the only difference is whether QApplication or QCoreApplication is used in your main. So you can change afterwards. However, you need to make sure that the also "GUI library" is checked under "Qt modules" when you change to QApplication. At least I believe this should be fine.
If you have a gui application but you like to see also "normal" output, because of debugging or so. You should check out "QDebug.":http://qt-project.org/doc/qt-4.8/qdebug.html
You can use it as a substitute for cout and cerr. This "console" output will be displayed in a special vc output window when running the debugger. -
@koahnig one more thing I want to ask is that , When I have qt visual studio add-in, do I need to mess with qmake and nmake etc ? , is cmake better option ?
Thanks
-
I do not fuzz around with any of these directly. All is handled by vc or the vsaddin. As long as you stay in that "world"/environment there is no need to bother with qmake and nmake.