Qt - Unresolved externals, again

I keep getting unresolved externals when I change code that uses Qt in VC++

There is no actual code missing, it relates to the generated code. If I delete the generated files and mess with it, sometimes it builds fine.

1
2
3
4
5
Error	1	error LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __thiscall MainWindow::metaObject(void)const " (?metaObject@MainWindow@@UBEPBUQMetaObject@@XZ)	rsMainWindow.obj	QtMyGraph
Error	2	error LNK2001: unresolved external symbol "public: virtual void * __thiscall MainWindow::qt_metacast(char const *)" (?qt_metacast@MainWindow@@UAEPAXPBD@Z)	rsMainWindow.obj	QtMyGraph
Error	3	error LNK2001: unresolved external symbol "public: virtual int __thiscall MainWindow::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@MainWindow@@UAEHW4Call@QMetaObject@@HPAPAX@Z)	rsMainWindow.obj	QtMyGraph
Error	4	error LNK2019: unresolved external symbol "public: static struct QMetaObject const MainWindow::staticMetaObject" (?staticMetaObject@MainWindow@@2UQMetaObject@@B) referenced in function "public: static class QString __cdecl MainWindow::tr(char const *,char const *)" (?tr@MainWindow@@SA?AVQString@@PBD0@Z)	rsMainWindow.obj	QtMyGraph
Error	5	fatal error LNK1120: 4 unresolved externals	C:\Users\Beakie\Documents\Visual Studio 2008\Projects\QtMyGraph\Win32\Release\QtMyGraph.exe	QtMyGraph



What is the correct method for getting these files to rebuild when needed without all the messing about?
I had a similar problem before - and it annoyed me for ages, then I realised that I had:

1
2
3
MyFunction() {

}


instead of :

1
2
3
MyClass::MyFunction() {

}


I am not sure whether this is the same problem. Hopefully you IDE copies function declarations over to definitions and puts the class & :: in there for you.

Failing all that, it looks like a problem with the tr function ( which does translations to other languages) - if yuo don't really need them, try removing them if you have no other solution. Try google he requirements for using tr.

If all else fails , try the Qt forum.

Hope this is helpful a little bit.
I haven't used QT for a while. What it looks like is that you didn't add the generated file to your project?
coder777 wrote:
What it looks like is that you didn't add the generated file to your project?


The Meta Object Compiler (MOC) should generate the files automatically - no user action is required at all.
Here is the info on moc:

http://doc.qt.digia.com/4.7-snapshot/moc.html


Maybe you don't have the Q_OBJECT at the top of your file?
That's the weird thing.

I don't have to change anything to get it to build correctly.

The generated files are the problem. They are included correctly.

If I were to add some code (not to the generated files, my files) and recompile, would these files be different?

If so, it seems as though these files are not updating or rebuilding when I compile?


It works SOME of the time, thats the most annoying bit!
If so, it seems as though these files are not updating or rebuilding when I compile?


Are you using qmake ? It is this program that does all the moc stuff automatically (provided you have the Q_OBJECT in your file.

If I were to add some code (not to the generated files, my files) and recompile, would these files be different?


Yes. The way I understand it from the web page in my last post, the moc takes care of the slots & signals, so if you add a function to implement a new slot, then this info will be included by the moc. Provided you don't make a silly mistake like I did, then this should work.


Qt reference:
The Meta-Object Compiler, moc, is the program that handles Qt's C++ extensions.

The moc tool reads a C++ header file. If it finds one or more class declarations that contain the Q_OBJECT macro, it produces a C++ source file containing the meta-object code for those classes. Among other things, meta-object code is required for the signals and slots mechanism, the run-time type information, and the dynamic property system.

The C++ source file generated by moc must be compiled and linked with the implementation of the class.

If you use qmake to create your makefiles, build rules will be included that call the moc when required, so you will not need to use the moc directly.


It works SOME of the time, thats the most annoying bit!


Another thought, have you tried doing a 'clean' followed by 'configure' then 'rebuild' ?

If you are keen on using Qt, consider downloading the Qt SDK, which is free, works on all the OS's and has the Qt Creator IDE, which is not surprisingly specifically for Qt.

It's not impossible to use other IDE's for Qt (I used KDevelop, a Linux IDE, with Qt for awhile), but it might be easier to use something that is designed for it.
Topic archived. No new replies allowed.