Search code examples
c++visual-studio

Visual Studio console debugging wrong file


I have created an empty project and added two ( .cpp) items inside the project. Keep in mind that I am a beginner in C++ Visual Studio, so I have not used any code to somehow connect the two files.

Problem: The debugger was fine for debugging my first file, but when I open my second file and start running by clicking "Local Windows Debugger" (clicked when I was inside the second file), it will still keep on running the first file whether there was a bug or not.

When I looked at the debug window after I hit "Local Windows Debugger", I saw the file path pointing to the first file.

I have tried: Closing the first file completely, closing visual studio and opening my second file from my folder path, turning off the break-point in the first file and turning on in the second file.

I would like to know: How can I just run the second file? Do I have to use the command prompt to keep my two items in the empty project separate? I am using Windows 10 by the way.

I searched for my problem, but I had a hard time looking for a guide that gave a solution


Solution

  • Issue:

    Dracep cannot debug one of two files they have. This is due to them having a main function definition in both files, causing the editor to favour one over the other.

    Solution:

    By having a third, dedicated entry point in your application (I.E. having a single point of entry you then include the other files), you can decide which file you are going to debug at any given time.

    For example, having a file called main.cpp which then includes the other two files by using #include "filename.h".

    From there you can include the file and make you code checks by calling the functions in that file rather than having a main and stepping down through it, causing long term issues of scalability.

    Please see this question on separating your logic from your definitions, as the answer marked correct is the standard for most C++ projects you will find.

    That way, you could do something like the following:

    #include "File1.h"
    #include "File2.h"
    
    int main(int argc, char** argv)
    {
        File1Class file1Class;
        File2Class file2Class;
        //Do whatever tests you like with either.
    }