Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt - Visual studio 2010 , Moc changing CPP file causes link error. forces me to rebuild moc files
Forum Update on Monday, May 27th 2025

Qt - Visual studio 2010 , Moc changing CPP file causes link error. forces me to rebuild moc files

Scheduled Pinned Locked Moved Qt Creator and other tools
18 Posts 4 Posters 14.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jolson
    wrote on last edited by
    #1

    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...

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • J Offline
        J Offline
        jolson
        wrote on last edited by
        #3

        Yes, I have installed the addin

        1 Reply Last reply
        0
        • K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          what do you mean with cpp tied to an ui ?

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jolson
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              koahnig
              wrote on last edited by
              #6

              You have only custom build steps for your .ui and your .h.
              For example you have

              • BrowserWindow.ui
              • BrowserWindow.h
              • BrowserWindow.cpp
                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.

              Vote the answer(s) that helped you to solve your issue(s)

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jolson
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • J Offline
                  J Offline
                  jolson
                  wrote on last edited by
                  #8

                  Adding the cpp as a dependancy does not work. Is there a way I can generate the moc files from the command line

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    koahnig
                    wrote on last edited by
                    #9

                    Typically you have somewhere the generatedfiles stored. Delete the ones, which you think need moc'ing.

                    Vote the answer(s) that helped you to solve your issue(s)

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      jolson
                      wrote on last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        koahnig
                        wrote on last edited by
                        #11

                        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.

                        Vote the answer(s) that helped you to solve your issue(s)

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          jolson
                          wrote on last edited by
                          #12

                          My outputs property for the header is
                          $(ConfigurationName)\moc_%(Filename).cpp;%(Outputs)

                          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.

                          1 Reply Last reply
                          0
                          • K Offline
                            K Offline
                            koahnig
                            wrote on last edited by
                            #13

                            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.

                            Vote the answer(s) that helped you to solve your issue(s)

                            1 Reply Last reply
                            0
                            • G Offline
                              G Offline
                              goetz
                              wrote on last edited by
                              #14

                              Changing mainwindow.cpp should not require moc_mainwindow.cpp to be recompiled. The moc file contains only stuff created out of the header file.

                              http://www.catb.org/~esr/faqs/smart-questions.html

                              1 Reply Last reply
                              0
                              • J Offline
                                J Offline
                                jolson
                                wrote on last edited by
                                #15

                                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?

                                1 Reply Last reply
                                0
                                • K Offline
                                  K Offline
                                  koahnig
                                  wrote on last edited by
                                  #16

                                  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:
                                  !http://db.tt/1OzWlfNv(GeneratedFilesSection)!
                                  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.

                                  Vote the answer(s) that helped you to solve your issue(s)

                                  1 Reply Last reply
                                  0
                                  • J Offline
                                    J Offline
                                    jolson
                                    wrote on last edited by
                                    #17

                                    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.

                                    1 Reply Last reply
                                    0
                                    • P Offline
                                      P Offline
                                      phyatt
                                      wrote on last edited by
                                      #18

                                      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.

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved