Debug Multi-File C++ Programs in VS Code and WSL (Windows)
HTML-код
- Опубликовано: 8 мар 2021
- tldr:
1) Put all your project code files into a single folder, and only have one main function.
2) Try debugging as normal to generate the .vscode folder and .json files
3) Go into tasks.json and modify the second entry of "args" under the task with labe "C/C++" g++ build active file." Change it from "${file}" to "*.cpp"
That's it. It should compile and catch your breakpoint. If you get an error of "redeclaration of main," that means you need to adhere to step 1, above. Кино
I like the way you start the video with what will happen if the viewer stops watching now. Then going on to show the solution. So much better than "show them what you are going to tell them, tell them, show them what you just told them.". Excellent video, thank you.
Thanks man this literally helped so much tried many ways but this worked and moreover your interaction in the comment solved my problem.😄😄
I extremely thanks to you since I stuck with this concept 3-4 hours and then I can get along with it😊
Thank you for making this video. I was going to give up on using the debugger XD. You have saved me hours of manual debugging
glad to hear it - let me know if there's any other kinds of videos you think would be helpful in programming
I cannot like this video enough. I messed around with CMAKE , and all kinds of other videos and this was the one that helped!
I cannot express how much appreciation I have for this man rn !
Thanks! I was trying to figure out a solution for hours!
Thank you so much, finally found a tutorial that makes sense.
I signed in to like this video just because of the description. Tysm
The video is very clear. The solution is neat and simple.
YOU FIXED MY PROBLEM. THANK YOU KING!
I've been searching for 2 days for a solution ....thanks a lot
Thank You For All Your Videos Thank
YES!!!! IT WORKED!!! THANK YOU SO MUCH!
Simplest solution ever...Thanks
thank you man, I'm a noob in vscode and those .json files scared me, you save me.
glad to hear this helped. Let me know if there are other videos that would be helpful. Haven't been on here much lately but I'm teaching again so could make some more videos if it's helpful.
Wow thank you so much ! I knew there was something wrong in the preLaunchTask command but couldn't figure out what exactly...
Thanks, that very helpful!!!
thank you for your helpful video 😁😁😁
Thank you sooooo very much!!!!
Thank you for sharing
Thank you!
THANK YOU SIR
THANK YOU SO MUCH!
Thx duuuuude
Thank you!!!
Thanks for the video, I have been searching for the same solution.
I am wondering how to work with subfolders with multiple files? is it possible? instead of *.cpp can we put something else to consider subfolder cpp files?
I haven't tested this but there are other comments on this video with suggestions for this.
For example:
"${fileDirname}\\*.cpp",
Excelent video but I have a question: when i click on the green box in the bottom left corner saying "WSL: Ubuntu", it doesn't give me the option "Reopen Folder with WSL". Do I need to install something? Thank you.
If it already says WSL: Ubuntu next to that icon down there, that means your window is already open in WSL so you can proceed.
but how do i do if i have files on different folders
eg . header files on a folder named "include"
driver on " src" and output on "build"?
Sorry, I don't know
I have the same question.
@@salman8562 you can refer my comment in this vid. Basically change it to **/*.cpp
@@adheesh2secondsago630 yyeah i figured that out, might make a video on youtube tbh because i couldn't find it anywhere. I then changed to cmake for better debugging
It says that it was built succesfully but then it says "Unable to start debbuging. The value of miDebuugerPath is valid. What should I do? I did exactly what you did on the video.
sudo apt-get update && sudo apt-get install gdb
try running that command in your terminal to install gdb (the debugger)
@@michaelhoefer thanks a lot, I will try it
mine is still not working
How to use a single header file with multiple source files? Like we do in Codeblocks? If that's possible...
Do you mean multiple source files in a single class? You should be able to add a #include to the header in each of the source files. I don't know what codeblocks is.
Unfortunately this method doesn't work on Ubuntu Linux.
wildcard do not work for some reason, *.cpp do not extend for me it just stays as '*.cpp' and it says there is no such file
It didn't work for me either. To make it work, I added the name of each file. Like this:
"args": [
"-fdiagnostics-color=always",
"-g",
"file1.cpp",
"file2.cpp",
"file3.cpp",
"-o",
It broke with the nevest update of C/C++ extension from microsoft. Roll back to previous version and wildcard works again. It is pain without wildcard. I recommend to disable all updates. @@Rafael-4ac
For me it doesn't work. I'm on Windows OS
thanx now i am able to debug but still when i run the code it shows undefined reference error
Is the undefined reference to a class you wrote in another file? What exactly does the error say? You have to make sure you #include "yourHeaderFile.h" in the .cpp file where you use the class.
@@michaelhoefer i include the header file in main.cpp and the other cpp file but it is saying that the undifened reference is in main to the member implementation in other .cpp file
Happens while i run the program when i debug the program i get the correct output ig something wrong with the settings
@@amanpandey4905 Oh you mean when you debug it's correct, when you compile/run without debugging you get the error. How are you compiling? You need to be sure to include the other .cpp file in your compile command. Such as: g++ main.cpp otherFile.cpp
@@michaelhoefer thanks resolved it
I don't have a launch.json file
go to run->start debugging. And just click through, and that will automatically create the launch.json for you. It will fail at first, but it will build you a launch.json that you can edit.
Here is a cool tip:
Learn Cmake, very extensive build system. It is also supported for VS code
This is great - thank you for sharing
Not working. "g++.exe: error: **/*.cpp: Invalid argument"
@@MsTim159 I edited everything.
@@adheesh2secondsago630
I figured it out. You need to manually specify the directories where the cpp files are located, like this: "${fileDirname}\\*.cpp", "${fileDirname}\\src\\*.cpp",
@@MsTim159 Yeah but learning a build system would make you more practical.