Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Qt - Visual studio 2010 , Moc changing CPP file causes link error. forces me to rebuild moc files
I am using QT VS 2010 for some reason if I make a change to a cpp file that is tied to a ui. I am forced to rebuild the moc files. If I don't I will get linker errors. I can live with the rebuild. The problem is The only way I am able to get the moc files to build is to re-create my vcproj.
Is there a way I can force moc files to be re-built every compile?
When I compile I do see the following message for each .h file Moc'ing xxx.h...
Do you have "vs-addin":http://qt.nokia.com/downloads/visual-studio-add-in installed ?
You need to have a licensed version of msvc for using vs-addin.
Yes, I have installed the addin
what do you mean with cpp tied to an ui ?
I have a MainWindow.ui file a mainWindow.h that defines Q_OBJECT and a mainwindow.cpp. Any change to mainwindow.cpp requires me to regenerate the moc file.
You have only custom build steps for your .ui and your .h.
For example you have
If you change BrowserWindow.cpp only this file requires compilation. If you change BrowserWindow.h this file will be moc'ed and BrowserWindow.cpp will be compiled. If you change the .ui file the ui file will be uic'ed and rest will follow too. That should be enough to my understanding.
However, if you want to force a moc'ing of .h you can go to the properties for that file. You might be able to add the additional dependency to .cpp.
I am using msvc 2005, so I do not know how this looks in vc 2010, but I assume that there is a possibility to do so.
Once again, I have checked in a project of mine. The dependencies are set that with a changed .cpp only this file is compiled.
I wouldn't expect a changed cpp file would require a rebuild, however if its not rebuilt I am seeing link errors. I will add the .cpp to the .h dependencies and see if that fixes the issue.
Adding the cpp as a dependancy does not work. Is there a way I can generate the moc files from the command line
Typically you have somewhere the generatedfiles stored. Delete the ones, which you think need moc'ing.
I've tried that, the problem is if I delete them the files are not re-created. which requires me to regenerate the vcproj. The Moc’ing xxx.h… is displayed for each header but nothing is generated.
OK. You need to check then the "Outputs" property for your header files then. It typically sais something like "./generatedfiles/" with some macros attached.
Other solution could be to remove the files in question from your project. Beware, to make a backup first. But that is probably what you mean with regenerate .vproj, isn't it?
I had to do this recently, but it works ever since.
My outputs property for the header is
When I say regenerate I open the project file (.pro) from the qt plug in which asks me if I want to recreate the vcproj file.
I do not use .pro files for msvc. Respectively, only for getting the project struture to qt creator on linux.
Can you trace the output? Are the files created where they are supposed to be created?
In msvc 2005 I removed (not delete!) the files and added them as existing files again. That cured recently my problem.
goetz last edited by
Changing mainwindow.cpp should not require moc_mainwindow.cpp to be recompiled. The moc file contains only stuff created out of the header file.
I figured out why I am having an issue, the problem is when I compile the moc_mainwindow.cpp exclude from build property is set to yes. So the next time I try to compile I get link errors.
Any idea why the exclude from build prop is getting set to yes?
The exclude from build is handled by vs addins I assume or it must be handled by msvc itself.
In msvc 2005 it looks like this:
This is for compilation in debug mode. The release mode shows the "excluded from build" stop sign at right. The debug section does not because that is debug mode. When switching to release mode it will be the other way around.
As explained above I had an issue there as well recently. I was removing from the project (do not delete the files) the adequate header, source and ui files and adding them to the project again with "add existing files". This procedure cured the problem.
Before that I was fiddling around with the properties but that confused only the whole build process even more.
Removing and re-adding the files didn't help. For now I can live with this, when I'm finished with my current project I think I will try a fresh install.
phyatt last edited by
I got stuck in this same problem earlier today. I tried build clean on my project and started getting the "c1xx : fatal error C1083: Cannot open source file: 'Release\moc_xxxx.cpp': No such file or directory" errors.
After much frustration with vs2010, I figured two things out:
I could get the moc_xxxx.cpp files to come back using the "Restore previous versions" options that came with my version of Windows. Every time you update windows, it takes a snapshot of your recently changed files and saves it on your harddrive.
After getting it to that point, I also found another "fix" but this is more of a hack than a fix. For every .h file that you want to force Moc'ing for, (so any of them with a Q_OBJECT macro in them) you can tell visual studio to explicitly build the .h file (right click on the file and choose compile, or press Ctrl-F7 while in the .h file).
Then the next time you build, it can find the Moc'ed file and the error goes away.
I hope that helps someone.
It would be nice if there was an option built into the Qt menu on the VS plug-in that could do that for you (such as "Regenerate All Moc Files") or if it could notice that the moc files don't exist and built them so that you can recover after a build clean.