skip to Main Content

Recently I wiped my pc and reinstalled visual studio community 2022.
Before re-installation, whenever I made some changes in my projects, I would press F5 to enter debug and it would build all files before entering debug mode.

To make it clear, I can manually build the files every time via pressing ctrl+F7, but it is just a work around.

Now, after making changes and pressing F5 it ignores those changes and runs the last build.

I went over the settings and made sure everything matches with another install I have on a second machine.

I found this post the source file is different from when the module was built and went over all suggested solutions – nothing helped.

I tried making a new project with the thought that maybe the project configuration was at fault… no results.

If you want to re-create this behavior, create a new C++ project and 2 files in it (main.cpp, module.cpp).
Here’s the code that I used (if you think the code it self causes the issue, please note it):

main.cpp

#include <iostream>
using namespace std;

#include "module.cpp"

int main()
{
    handler h;

    h.func1();

    return 0;
}

module.cpp

#include <iostream>
using namespace std;

class handler
{
public:
    void func1()
    {
        cout << "idk" << endl;  // change this line
    }
};

Try debugging this setup, for the first time it will build all files since they’re new.
Afterwards change the marked line, and debug again (without manually building).

Placing a breakpoint in main.cpp and following into func1() will either work smoothly or result in the behavior I described.

Here are screenshots if anyone needs them.
open file popup
source not found tab

2

Answers


  1. There’s an option for that (of course!).

    You want Menu -> Tools -> Options -> Projects and Solutions -> Build and Run, and then select ‘Prompt to build’ from the ‘On Run, when projects are out of date’ dropdown.

    Pertinent screenshot:

    Visual Studio Screenshot

    Login or Signup to reply.
  2. Possible cause 1

    I think it’s because your file module.cpp is seen as a text file and not as a source file by VS (Visual studio), and therefore VS won’t rebuild your project after it is changed.

    If it’s a source file it should be refered in YourProject.vcxproj as:

      <ItemGroup>
        <ClCompile Include="module.cpp" />
      </ItemGroup>
    

    So, in order to add new source files cleanly you want to use the VS’s built-in "add class" (shift+alt+c) and use those files.

    In your case, your can repair your project by editing your vcxproj file as in my example.

    Possible cause 2

    Including directly the cpp file can be problematic: cpp are compiled (as refered in the project file), including it will copy its content, leading to duplicates in the binaries if it’s included more than once, so you can’t use it several times (but you can include the .h several times, the .cpp being compiled once) ! hence in general, stick to the .h and .cpp pair canon.

    Instead you want to have a .h .cpp pair, and put your declaration in .h and your code in either of them, and include the .h in your main source.

      <ItemGroup>
        <ClCompile Include="module.cpp" />
        <ClCompile Include="main.cpp" />
      </ItemGroup>
      <ItemGroup>
        <ClInclude Include="module.h" />
      </ItemGroup>
    

    In that case, the binaries were updated when the source were, when I asked to debug (F5).

    module.h:

    #pragma once
    #include <iostream>
    using namespace std;
    
    class module
    {
    public:
        void func1()
        {
            cout << "Now it works !" << endl;  // change this line
        }
    };
    

    module.cpp:

    #include "module.h"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search