Debug Multi-File C++ Programs in VS Code and WSL (Windows)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone it's michael here i'm going to do a quick example of how to debug a multi-line program or multi-file program in c plus with vs code so i've set up my files all in the same folder i've got my header file for my concert class my concert implementation file and then a driver which basically just creates an instance of the concert sets some data members and then prints out statistics so the question is i want to debug i want to set my breakpoint here and i want to be able to debug and step through my program so of course the first thing we'll try to do is reopen the folder in wsl like we always need to do when we're debugging using windows subsystem for linux so we'll give this just a second to open up okay so [Music] let's just see what happens when i set my break point and i hit f5 oh okay it can be a little bit finicky at times okay try this again we'll do gdb and we'll run g plus plus all right so we get some errors here let's see exactly what's going on what did they tell us so they say okay undefined reference to concert concert this is my constructor so we're seeing we don't have a definition of this function that i'm trying to call i try to call that right here there's no function in other words they don't know where the implementation is for this c plus plus we just compiled this file the consort driver and we need to include the concert implementation file when we compile so how do we do that let's go into vs code let's take a look at the launch.json first let's look at the tasks so we see when we do this run start debugging what we actually are doing is this uh oh well no hold on one second ah okay so we do this build and debug in the launch this is the program that we want to run that's okay and what we're doing here is we call this pre-launch task build active file so we want to do is we want to modify this pre-launch task to include compiling not only our consort driver but also our consort.cpp folder file so in our tasks we find this task and this is what we want to modify so here we see okay here's the arguments to our g plus plus compiler this is where we would normally do our file name and the problem is that we're only passing it a single file we're only passing at this concert driver.cpp we also need to pass it the concert.cpp so so what i'm actually going to do is i'm actually just going to do star.cpp which will include every cpp file in the compilation now this only works if you have your debugger scoped to a single folder right so if i look at my structure if i look at my project structure here what product what folder is this class debug okay let me pull that up real quick so my class debug so i have my top level csci 1300 folder and for every single multi-file program i want to compile i give it its own folder this one i'm calling class debug could have called it concert whatever and so this vs code folder is local just to this folder which means that when i go to compile it's going to scan this directory include all of the cpp files now if you have all your files in one huge directory this clearly isn't going to work because you're going to get an error about redefining the main function so you need to make sure you only have one main function across all the files you try to compile okay so now that i've changed this here i'm going to save that i need to go back to the driver that i want to run and let's try to hit f5 again notice how we see our command for g plus plus now we're compiling all of those c plus files and our debugger should allow us to step into our main function and we should be able to debug line by line and here we go okay so now we're paused and now we can we can see we can use our step over and step in i'm going to use my step in function just to show that we can actually go into different files so this is my cpp implementation file okay now i can step over i see my this is the new instance of my class that i'm creating and these are the default values i'm setting in my default constructor okay so i've got all these set to the basic values and i return back and now i'm going to set these values one at a time using my different setter functions so i first set the capacity the name the price of course by the way you can step into these functions if you'd like as well they're back in the same file they're very simple i just said the value passed in into the object data member we'll step over and then we'll print out our stats at the end cool all right so the key takeaways here you want to make sure that each multi-line pro project has its own folder and you simply need to go in make sure you're using a wsl ubuntu always click this reopen folder in wsl so you get this symbol down here first thing you want to do is try to start debugging and it'll give you that error and that will create these files for you and then in the tasks.json under c plus build active file go down and modify this argument after the dash g to be star.cpp which will include all of your cpp files in your compilation which means this build task will be successful allowing you to debug multifile programs okay if you have any questions feel free to comment below or shoot me an email hope this helps thank you
Info
Channel: Michael Hoefer
Views: 21,528
Rating: undefined out of 5
Keywords:
Id: DVyYnOHP4nY
Channel Id: undefined
Length: 7min 23sec (443 seconds)
Published: Wed Mar 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.