A problem related to meta-object compiler(MOC)

· programming

Last night I was studying tutorial 7, trying to run the codes in VS2010 environment, a problem emerged:
LCDRange.obj : error LNK2001: unresolved external symbol “public: virtual struct QMetaObject const * __thiscall LCDRange::metaObject(void)const “

Tutorial 7 is different from tutorial 6 only by adding a user defined signal and slot, which means the signal/slot suite requires special linking strategies. I found a very good thread providing general information and a piece of a Chinese blog suggesting a simple but effective solution . It seems that QT has a fundamental mechanism for signals and slots, which is called the Meta-Object System (get a better understanding by reading the QT Documentation). Simply compiled by cl compiler/linker, the external symbols couldn’t be recognized correctly, which led errors to occur.

In summary, when defining a self-defined signal/slot, the following should be done:

1. Declare the new class in a separate header file.

2. Always remember to contain the “Q_OBJECT” macro at the beginning of class declaration (read the QT Documentation on Using the Meta-Object Compiler/MOC).

3. Compile the header files using MOC with following syntax: moc xxx.h -o moc_xxx.cpp

4. Add moc_xxx.cpp into the project.

Then build! It should work now~



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: