Qt World Summit: Register Today!

[SOLVED] Unresolved External Symbol Error

  • Hi All,

    I am getting three errors when I try to pass an object to another class. The errors are:

    film.obj:-1: error: LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Film::metaObject(void)const " (?metaObject@Film@@UEBAPEBUQMetaObject@@XZ)

    film.obj:-1: error: LNK2001: unresolved external symbol "public: virtual void * __cdecl Film::qt_metacast(char const *)" (?qt_metacast@Film@@UEAAPEAXPEBD@Z)

    film.obj:-1: error: LNK2001: unresolved external symbol "public: virtual int __cdecl Film::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@Film@@UEAAHW4Call@QMetaObject@@HPEAPEAX@Z)

    All have "File not found: film.obj" under the errors in red.

    The class that I am trying to pass:

    @class Film: public QObject

    Q_PROPERTY(QString title READ getTitle  WRITE setTitle)

    Q_PROPERTY(QString director READ getDirector WRITE setDirector )
    Q_PROPERTY(int duration READ getDuration WRITE setDuration )
    Q_PROPERTY(QDate releaseDate READ getReleaseDate WRITE setReleaseDate)

    Film(); // Default constructor
    Film(QString t,int dur, QString dir, QDate r ); // Constructor
    void setTitle(QString t);
    void setDuration (int dur);
    void setDirector (QString dir);
    void setReleaseDate (QDate r);

    QString getTitle();
    QString getDirector();
    int getDuration();
    QDate getReleaseDate();

    QString title;
    int duration;
    QString director;
    QDate releaseDate;


    The class that has to receive it:

    Header file:

    @#include <QtCore>
    #include <QTextStream>
    #include <QFile>
    #include <QString>
    #include "film.h"

    class FilmWriter

    FilmWriter(Film * myFilm);



    The CPP File:

    @FilmWriter::FilmWriter(Film * myyFilm){

    //Film myFilm(myyFilm);
    QString mFileName = "F:/myFilms.txt"; //File to store the Films
    QFile mFile&#40;mFileName&#41;;
    if (!mFile.open(QFile::WriteOnly | QFile::Text&#41;)
        qDebug()<< "Cannot open File";
    QTextStream out(&mFile);




    out<< myFilm.getTitle();
    out<< myFilm.getDirector();
    out<< myFilm.getDuration();


    Please help.


  • Lifetime Qt Champion


    Did you re-run qmake after adding Q_OBJECT to your Film class ?

  • Sorted. Thanks for the help.

  • Lifetime Qt Champion

    You're welcome,

    Don't forget to update the thread's title prepending solved so other forum users may know a solution has been found :)

  • Hi, Im new in QT. Can you write more informations how to re-run qmake .
    I have one more question. Is the qmake file the same as MyProject.pro ?? - the .pro file in my project ?

  • Lifetime Qt Champion

    Hi and welcome
    Yes MyProject.pro is the qmake file.

    • To re-run it means to go to build menu and select "Run qmake" to be sure.

    In rare cases , you might also delete your build folder for things to be really remade.
    To find build folder, look in "projects" tab, and its just Build directory.
    This folder contains auto generated files and safe to delete all files in.
    So you exploer to go there and delete all in those cases.

  • Oh, my mistake. I forgot say, that Im working in VisualStudio. There is not something like "Run qmake". Is there any way to do this in Visual Studio ?

  • Lifetime Qt Champion

    Oh. I never used VS with Qt so Im not sure.

    Normally you also install the Qt VS plugin that handles all this.

    Since VS dont really use .pro files i assume you have this plugin as
    you sound you can compile.

    Did you install this plugin ?

    If not, you will have that issue that Qt want a tool called moc.exe run on normal compiles.
    This tools makes connect and a lot of other core features work.

    If its not run, then you will see errors like in this thread.

    What version of VS are you using ?
    Make sure it all matches with version etc.

  • @Lordon
    You don't need to run qmake if using Visual Studio with the Qt VS plugin, as VS will set up custom build steps for your .ui files (to run uic) and .h files that use Q_OBJECT (to run moc).

  • I have the same issue too, but with Visual Studio + Cmake setup.
    There is something that cmake does about the moc:
    [1/5] Automatic MOC and UIC for target MyProject
    [2/5] Building CXX object Test05\CMakeFiles\MyProject.dir\MyProject_autogen\mocs_compilation.cpp.obj
    [3/5] Building CXX object Test05\CMakeFiles\MyProject.dir\src\Main.cpp.obj
    [4/5] Building CXX object Test05\CMakeFiles\MyProject.dir\src\MainWindow.cpp.obj
    [5/5] Linking CXX executable bin\MyProject.exe

    and the same errors in linking stage

    In the build directory, after some searching, I found only a file called "mocs_compilation.cpp", but that file contains only

    // This file is autogenerated. Changes will be overwritten.
    // No files found that require moc or the moc files are included
    enum some_compilers { need_more_than_nothing };

    If I understand it correctly - moc should create an Q_OBJECT macro implementation for my class and that should be linked in final executable, but that file is not generated

    UPD. A solution found
    I also needed to add header files as source files in cmake add_library/add_executable functions, e.g. add_executable(MyProject "include/HeaderFileContaining_Q_OBJECT_Macro.hpp"). It seems weird because there is no need to "include" headers that way, it just doesn't make sense in non-qt cmake development

Log in to reply