13

VSCode Version:1.3.1

OS Version:Ubuntu 14.04

I debug a C++ project on Ubuntu 14.04. I run cmake to produce an executable file and setting VSCode config file. When I press F5 to debug, the program runs well, but it doesn't stop at breakpoint!

my source code is in ${workspaceRoot}/InfiniTAM

executable file is in ${workspaceRoot}/build

My config file:

tasjs.json

    {
"version": "0.1.0",    
"command": "echo",    
"isShellCommand": true,    
"args": ["InfiniTAM!"],    
"showOutput": "always"    
}

launch.json    
{
    "version": "0.2.0",    
    "configurations": [    
        {    
            "name": "C++ Launch (GDB)",    
            "type": "cppdbg",    
            "request": "launch",
            "launchOptionType": "Local",    
            "targetArchitecture": "x64",    
            "program": "${workspaceRoot}/build/InfiniTAM",    
            "args": ["Teddy/calib.txt", "Teddy/Frames/%04i.ppm","Teddy/Frames/%04i.pgm"],    
            "stopAtEntry": false,    
            "cwd": "${workspaceRoot}/build",    
            "environment": [],    
            "externalConsole": true
        }
    ]
}
Benjamin Zach
  • 1,452
  • 2
  • 18
  • 38
李仕杰
  • 131
  • 1
  • 1
  • 4

3 Answers3

40

Watch out, how you compile your code:
In order to be able to debug you need to compile e.g. with g++ with the flag -g.

Benjamin Zach
  • 1,452
  • 2
  • 18
  • 38
3

I have had this same problem for a while and am now able to write a working configuration which works universally for all C and CPP programs and you don't need to change anything during execution.

  • Install Code-Runner Extention.
  • Paste the configuration given below into your settings.json file.

     {
       "code-runner.executorMap": {
    
        "javascript": "nodejs",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc -g \"$fileName\" -o \"$fileNameWithoutExt\" && $dir\"$fileNameWithoutExt\"",
        "cpp": "cd $dir && g++ -g \"$fileName\" -o \"$fileNameWithoutExt\" && $dir\"$fileNameWithoutExt\"",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python3",
        "perl": "perl",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "kotlin": "cd $dir && kotlinc $fileName -include-runtime -d $fileNameWithoutExt.jar && java -jar $fileNameWithoutExt.jar",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run"
    }
    }
    
  • Paste the below code in launch.json by clicking the settings symbol in the debugging panel (present at top left).

    {
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
      ]
    }
    
  • After this When ever you press ctrl+Alt+N it compiles and executes and you can see the output in the output panel.

  • When You Press F5 after selecting the right Task ( choose (gdb)Launch ). It will start the debugging with working break points and stuff.

aksh1618
  • 2,245
  • 18
  • 37
Natesh bhat
  • 12,274
  • 10
  • 84
  • 125
  • 7
    This helped me realize I was missing the `-g` arg in the compile command, and how to use filename with gdb. – aksh1618 May 22 '18 at 14:31
1

I am pretty sure that

"cwd": "${workspaceRoot}/build"

is not right because "cwd" should contain the path to your source code. Otherwise breakpoints cannot be mapped from source code to your program.

Did you try to change it to

"cwd": "${workspaceRoot}/InfiniTAM"

?

Currently I am also having debugging issues with VSCode and C which could also relate to your problem. Therefore I may update my post soon with a link to my issue.

LFish
  • 299
  • 1
  • 6
  • 16