PDA

View Full Version : Updated to Creator 4.14 and 5.15.2 MSVC 2019 - now unable to generate main.moc



luckachi
28th December 2020, 19:49
I recently updated my Qt Creator to 4.14 and went from 5.14.2 MSVC2017 64bit to 5.15.2 MSVC2019 64 bit. My project had its command line arguments wiped out upon that update and now when I try to compile / run the app - I am receiving the following error:

error: dependent 'debug\main.moc' does not exist.

I have
#include "main.moc" on the last line of my main.cpp file

I have cleaned, ran qmake, built over and over again yet I can't seem to solve this issue (which did not happen while using MSVC2017) I use the Network Download example but have most of the code in a separate .cpp file versus having everything in my main.cpp. Nothing has changed in the code from using MSVC2017 and MSVC2019 so I am not quite sure why this would stop working?

Any assistance would be greatly appreciated.

d_stranz
28th December 2020, 21:16
Any assistance would be greatly appreciated.

Why would you have a "main.moc" file in the first place? .moc files are typically generated by the MOC compiler when processing a header file that contains the declaration of a class derived from QObject and which contains the Q_OBJECT macro. These types of classes are not typically declared in a .cpp file, especially main.cpp. Usually main.cpp is just a few lines of code containing the main() function that kicks off the app's execution.

This was probably an error you made long ago when first making the project, something that left behind a main.moc file in your debug build directory that didn't get removed during a make clean or rebuild. I am pretty sure that if you simply comment out that line, your project will build, and if it does, delete the line altogether.


versus having everything in my main.cpp

Edit - just noticed this. This almost certainly points to a previous version of the project where you had a QObject-based class defined in main.cpp that left behind some junk when you moved the class declaration out into its own file.

luckachi
29th December 2020, 13:16
Why would you have a "main.moc" file in the first place? .moc files are typically generated by the MOC compiler when processing a header file that contains the declaration of a class derived from QObject and which contains the Q_OBJECT macro. These types of classes are not typically declared in a .cpp file, especially main.cpp. Usually main.cpp is just a few lines of code containing the main() function that kicks off the app's execution.

This was probably an error you made long ago when first making the project, something that left behind a main.moc file in your debug build directory that didn't get removed during a make clean or rebuild. I am pretty sure that if you simply comment out that line, your project will build, and if it does, delete the line altogether.


This came from the Network Download example (https://doc.qt.io/qt-5/qtnetwork-download-example.html) where all of the code is placed in the main.cpp file with #include main.moc at the bottom. I ended up moving all the contents from the Network Download example into its own .cpp file (filedownloader.cpp) to keep everything separate and never had an issue with the project running so I thought I had done everything correctly. It wasn't until I updated everything where I started to run into issues.

When I just comment out #include "main.moc" I receive the following errors:

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

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

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

debug\misin.exe:-1: error: LNK1120: 3 unresolved externals


I tried to move the lines below to a separate header file (filedownloder.h) but I receive an error that reads Download of {file names} failed: Error while downloading : Size failed: Unable to find file



#include <QtCore>
#include <QtNetwork>

#include <cstdio>

QT_BEGIN_NAMESPACE
class QSslError;
QT_END_NAMESPACE

using namespace std;

class DownloadManager: public QObject
{
Q_OBJECT
QNetworkAccessManager manager;
QVector<QNetworkReply *> currentDownloads;

public:
DownloadManager();
void doDownload(const QUrl &url);
static QString saveFileName(const QUrl &url);
bool saveToDisk(const QString &filename, QIODevice *data);
static bool isHttpRedirect(QNetworkReply *reply);

public slots:
void execute();
void downloadFinished(QNetworkReply *reply);
void sslErrors(const QList<QSslError> &errors);
};



Everything was working fine until this update :(

d_stranz
29th December 2020, 16:37
Everything was working fine until this update

Almost certainly because at some point you built your project when all of the code was still in main.cpp, it created the main.moc file, and that file was still hanging around.

What is happening in this case seems to be that either MOC is not processing the header file where you have defined the DownloadManager class, 2) the cpp file generated by the MOC compiler is not being compiled by C++, or 3) the obj file produced by the compiler is not being linked into the executable.

Make sure that your Qt Creator .pro file contains the name of your DownloadManager class header file in the HEADERS line, and then run qmake again before rebuilding.

luckachi
29th December 2020, 17:22
Almost certainly because at some point you built your project when all of the code was still in main.cpp, it created the main.moc file, and that file was still hanging around.

What is happening in this case seems to be that either MOC is not processing the header file where you have defined the DownloadManager class, 2) the cpp file generated by the MOC compiler is not being compiled by C++, or 3) the obj file produced by the compiler is not being linked into the executable.

Make sure that your Qt Creator .pro file contains the name of your DownloadManager class header file in the HEADERS line, and then run qmake again before rebuilding.

Gotcha - that totally makes sense! I will fiddle around with this a bit more and see if I can work this out.

Added after 25 minutes:

I ended up creating a new header and .cpp file for the file downloader and deleting the old stuff. Seems to be working now - apologies for wasting your time @d_stranz - thank you for the clarification.

d_stranz
29th December 2020, 19:29
apologies for wasting your time

Never a waste of time. Sometimes the build process gets hung up with bad information or has inexplicable things happen because of some of the "hidden" steps that take place in between C++ code and the final result.