0

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

The_L0L
  • 11
  • 7
  • Why are you `#include`ing .cpp files? (Although I would expect this to work). – Paul Sanders Dec 16 '22 at 22:02
  • @PaulSanders I include the cpp file to use its contents, usually you would go for header/code structure but for this example it would just make more mess. – The_L0L Dec 17 '22 at 16:04
  • Maybe if you make it a `.h` file then VIsual Studio will behave correctly. You can still put code in there. – Paul Sanders Dec 17 '22 at 16:48

2 Answers2

1

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"
Soleil
  • 6,404
  • 5
  • 41
  • 61
  • When recreating this behavior I made sure to use the built-in functions to create the files from within visual studio, but I will verify right now if it solves the issue. – The_L0L Dec 16 '22 at 18:28
  • Just checked it, .vcxproj file contains reference to main.cpp and module.cpp in this manner. – The_L0L Dec 16 '22 at 18:30
  • I will check if it helps, though, if it does a question rises... why can't I include the cpp without worrying about creating a header file? – The_L0L Dec 17 '22 at 16:06
  • @The_L0L I detailed this point in the answer. – Soleil Dec 17 '22 at 19:20
  • 1
    Alright, got it! I don't know why I thought it will work, now that you mention it, the compiler plugs in the code from those included files and creating duplicates... Thanks for the detailed answer! – The_L0L Dec 18 '22 at 17:53
0

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

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • As I mentioned, changing this setting doesn't affect anything. Prompt/Always doesn't seem to work as intended. – The_L0L Dec 16 '22 at 18:21