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.
  • КиноКино

Комментарии • 63

  • @willofirony
    @willofirony Год назад +2

    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.

  • @parasrawat3173
    @parasrawat3173 2 года назад

    Thanks man this literally helped so much tried many ways but this worked and moreover your interaction in the comment solved my problem.😄😄

  • @thanhcong1349
    @thanhcong1349 9 месяцев назад

    I extremely thanks to you since I stuck with this concept 3-4 hours and then I can get along with it😊

  • @keshavgopinath
    @keshavgopinath 2 года назад +1

    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

    • @michaelhoefer
      @michaelhoefer  2 года назад +2

      glad to hear it - let me know if there's any other kinds of videos you think would be helpful in programming

  • @ALBOIN85
    @ALBOIN85 6 месяцев назад

    I cannot like this video enough. I messed around with CMAKE , and all kinds of other videos and this was the one that helped!

  • @rajraikar5176
    @rajraikar5176 Год назад +3

    I cannot express how much appreciation I have for this man rn !

  • @khaixa_
    @khaixa_ Год назад

    Thanks! I was trying to figure out a solution for hours!

  • @spadezap9728
    @spadezap9728 2 года назад +2

    Thank you so much, finally found a tutorial that makes sense.

  • @mikoajczermak6372
    @mikoajczermak6372 Год назад

    I signed in to like this video just because of the description. Tysm

  • @jzhang8137
    @jzhang8137 10 месяцев назад

    The video is very clear. The solution is neat and simple.

  • @proxy8918
    @proxy8918 2 года назад

    YOU FIXED MY PROBLEM. THANK YOU KING!

  • @ahmedosama1457
    @ahmedosama1457 Год назад

    I've been searching for 2 days for a solution ....thanks a lot

  • @os6348
    @os6348 Год назад

    Thank You For All Your Videos Thank

  • @summerishere5146
    @summerishere5146 7 месяцев назад

    YES!!!! IT WORKED!!! THANK YOU SO MUCH!

  • @user-wo6jr6jy4p
    @user-wo6jr6jy4p Год назад

    Simplest solution ever...Thanks

  • @diegomora997
    @diegomora997 Год назад

    thank you man, I'm a noob in vscode and those .json files scared me, you save me.

    • @michaelhoefer
      @michaelhoefer  Год назад

      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.

  • @iljastepanov2627
    @iljastepanov2627 2 года назад

    Wow thank you so much ! I knew there was something wrong in the preLaunchTask command but couldn't figure out what exactly...

  • @ketan8498
    @ketan8498 3 месяца назад

    Thanks, that very helpful!!!

  • @My.Daisy.
    @My.Daisy. 2 года назад

    thank you for your helpful video 😁😁😁

  • @note2976
    @note2976 Год назад +1

    Thank you sooooo very much!!!!

  • @QuangTran-qg6ri
    @QuangTran-qg6ri 2 года назад

    Thank you for sharing

  • @reshawnlofters
    @reshawnlofters Год назад

    Thank you!

  • @Itay12353
    @Itay12353 2 года назад

    THANK YOU SIR

  • @hexinhu2591
    @hexinhu2591 2 года назад

    THANK YOU SO MUCH!

  • @aslanserafimov7141
    @aslanserafimov7141 Год назад

    Thx duuuuude

  • @gabrieleladelfa2220
    @gabrieleladelfa2220 2 года назад

    Thank you!!!

  • @kvnptl4400
    @kvnptl4400 27 дней назад

    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?

    • @michaelhoefer
      @michaelhoefer  23 дня назад +1

      I haven't tested this but there are other comments on this video with suggestions for this.
      For example:
      "${fileDirname}\\*.cpp",

  • @diogofranco1288
    @diogofranco1288 Год назад

    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.

    • @michaelhoefer
      @michaelhoefer  Год назад

      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.

  • @anishmanandhar1203
    @anishmanandhar1203 3 года назад +2

    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"?

    • @michaelhoefer
      @michaelhoefer  3 года назад

      Sorry, I don't know

    • @salman8562
      @salman8562 2 года назад

      I have the same question.

    • @adheesh2secondsago630
      @adheesh2secondsago630 2 года назад +3

      @@salman8562 you can refer my comment in this vid. Basically change it to **/*.cpp

    • @salman8562
      @salman8562 2 года назад +3

      @@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

  • @user-ry7ms1kr4d
    @user-ry7ms1kr4d Год назад +1

    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.

    • @michaelhoefer
      @michaelhoefer  Год назад +1

      sudo apt-get update && sudo apt-get install gdb

    • @michaelhoefer
      @michaelhoefer  Год назад +1

      try running that command in your terminal to install gdb (the debugger)

    • @user-ry7ms1kr4d
      @user-ry7ms1kr4d Год назад

      @@michaelhoefer thanks a lot, I will try it

  • @sarfrajsingh1772
    @sarfrajsingh1772 2 года назад +1

    mine is still not working

  • @ayushdubey8745
    @ayushdubey8745 2 года назад

    How to use a single header file with multiple source files? Like we do in Codeblocks? If that's possible...

    • @michaelhoefer
      @michaelhoefer  2 года назад

      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.

  • @djura6303
    @djura6303 2 года назад +1

    Unfortunately this method doesn't work on Ubuntu Linux.

  • @recklessvelociraptor2579
    @recklessvelociraptor2579 4 месяца назад

    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

    • @Rafael-4ac
      @Rafael-4ac 4 месяца назад

      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",

    • @recklessvelociraptor2579
      @recklessvelociraptor2579 4 месяца назад

      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

  • @pinielchitare9221
    @pinielchitare9221 Год назад

    For me it doesn't work. I'm on Windows OS

  • @amanpandey4905
    @amanpandey4905 3 года назад

    thanx now i am able to debug but still when i run the code it shows undefined reference error

    • @michaelhoefer
      @michaelhoefer  3 года назад

      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.

    • @amanpandey4905
      @amanpandey4905 3 года назад

      @@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

    • @amanpandey4905
      @amanpandey4905 3 года назад

      Happens while i run the program when i debug the program i get the correct output ig something wrong with the settings

    • @michaelhoefer
      @michaelhoefer  3 года назад +2

      @@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

    • @amanpandey4905
      @amanpandey4905 3 года назад +1

      @@michaelhoefer thanks resolved it

  • @vaishanthjv2519
    @vaishanthjv2519 Год назад

    I don't have a launch.json file

    • @michaelhoefer
      @michaelhoefer  Год назад

      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.

  • @adheesh2secondsago630
    @adheesh2secondsago630 2 года назад +7

    Here is a cool tip:
    Learn Cmake, very extensive build system. It is also supported for VS code

    • @michaelhoefer
      @michaelhoefer  2 года назад

      This is great - thank you for sharing

    • @MsTim159
      @MsTim159 2 года назад

      Not working. "g++.exe: error: **/*.cpp: Invalid argument"

    • @adheesh2secondsago630
      @adheesh2secondsago630 2 года назад

      @@MsTim159 I edited everything.

    • @MsTim159
      @MsTim159 2 года назад

      @@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",

    • @adheesh2secondsago630
      @adheesh2secondsago630 2 года назад

      @@MsTim159 Yeah but learning a build system would make you more practical.