Skip to content

will4614/VSCode_C_Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VSCode_C_Example

The following steps have already been done for you in this project. You may need to do this in future projects.

How to setup VSCode to work with Makefiles and gdb.

Microsoft Documents

https://code.visualstudio.com/docs/cpp/cpp-debug

https://code.visualstudio.com/docs/cpp/launch-json-reference

VS Code Keyboard Shortcuts

Tabs

Set tabs to 2:

File | Preferences | Settings Search for tabs in the search box.

Set tab size to 2

Make sure you have User selected to set this for all projects on this machine.

Extension

Make sure the C/C++ for Visual Studio Code extension from Microsoft is installed. https://github.com/microsoft/vscode-cpptools

Setup the Debugger

Choose the Debug Icon (above) and select "Create a launch.json file" then "Default configuration"

Edit launch.json to contain:

"configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "${workspaceFolder}/bin/simpleCProgram", # THIS IS IMPORTANT
      "args": [],
      "stopAtEntry": true,  # THIS IS IMPORTANT
      "cwd": "${workspaceFolder}",
      "environment": [],
      "externalConsole": false, 
      "MIMode": "gdb",
      "setupCommands": [
        {
          "description": "Enable pretty-printing for gdb",
          "text": "-enable-pretty-printing",
          "ignoreFailures": true
        }
      ],
      "preLaunchTask": "runMake", # THIS IS IMPORTANT

    }
  ]

Now we need to make a task, runMake, that will run our Makefile.

Terminal | Configure Default Build Task | Create tasks.json from template | Others

Edit tasks.json to contain:

{
  "type": "cppbuild",
  "label": "runMake",
  "command": "/usr/bin/make",
  "args": [ ],
  "options": {
    "cwd": "${workspaceFolder}"
   },
   "problemMatcher": [
    "$gcc"
   ],
   "group": {
      "kind": "build",
      "isDefault": true
    },
    "detail": "Task generated by me."
}

Save all .json files.

Run the build process from the keyboard

Control-Shift-B will run the Default Build Command (runMake from above!)

Run the build process and launch the Debugger from the User Interface

Run/Debugger icon: (bug, triangle) press green triangle at top "(gdb) Launch".

Hit "run to next break point" (blue triangle with line)

Ouput shows up in terminal.

Let's add a debug configuration that runs our program with command line options.

Open launch.json (found in .vscode directory) Click Add Configuration (Blue Button) C/C++: (gdb) Launch

Edit to look like:

{
  "name": "(gdb) Launch with args", // Change name
  "type": "cppdbg",
  "request": "launch",
  "program": "${workspaceFolder}/bin/simpleCProgram", // THIS IS IMPORTANT
  "args": ["CS360"],  // add comma separated list of args here.
  "stopAtEntry":  true, // THIS IS IMPORTANT 
  "cwd": "${fileDirname}",
  "environment": [],
  "externalConsole": false,
  "MIMode": "gdb",
  "setupCommands": [
    {
      "description": "Enable pretty-printing for gdb",
      "text": "-enable-pretty-printing",
      "ignoreFailures": true
    }
  ],
  "preLaunchTask": "runMake", // THIS IS IMPORTANT
},

Run/Debugger icon: (bug, triangle) press drop down arrow near green triangle at top to select: "(gdb) Launch with args".

Press green arrow

Hit "run to next break point" (blue triangle with line)

Ouput shows up in terminal.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors