Visual Studio Code

Manish Chawda 431 Reputation points
2021-02-01T14:01:05.72+00:00

Hi,

I am new to Visual Studio Code and I am learning C programming. I am facing an error while compiling which says that I need to configure VS code for C11 / C99 mode. Kindly advise where to configure the same.

Also I am not able to debug. Kindly advise where to configure the same.

Also how to configure VS Code for C programming to compile with Windows debugger instead of GDB ?

How to configure VS Coed for C programming so that it used MSVC instead of GCC ?

Regards,

Manish Chawda

Community Center Not monitored
{count} votes

6 answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-02-03T15:23:43.323+00:00

    There is no benefit in reinstalling VS Code. There is nothing wrong with the installation. Ok, let's start at the beginning and note that I'm basically following the steps given here.

    Assumptions:

    • You already have a folder with your source files in them.
    • You are labeling your files .c to clarify this is C files and not C++. This has a minor impact on the build files later.
    • You have already installed Visual Studio 2019 or the Visual Studio 2019 Build Tools so you have access to the C++ debugger and compilers.

    Now the steps.

    1. You must start VS Code from the Developer Command Prompt that is generated when you install VS. This is how VS Code will be able to locate the MSVC components it needs. For now go that route. Later you can probably find the documentation that discusses how to configure the paths and run the same commands as the command prompt (it is just a shell script) so you can forgo this extra step. But let's just get it working for now.
    2. In VS Code open the folder containing your project using File\Open Folder. This will ensure all files are stored in the correct spot.
    3. Invoke the Command Palette by right clicking inside the source file and selecting that option. Then type Configuration and choose the option to edit the C/C++ configuration. Note that this is editing the C/C++ settings for only this folder, not globally.
    4. It should auto-generate the c_cpp_properties.json file. This is where you would change your language option from c17 to c11 or whatever you want to use. {
      "configurations": [
      {
      "name": "Win32",
      "includePath": [
      "${workspaceFolder}/**",
      "${vcpkgRoot}/x86-windows/include"
      ],
      "defines": [
      "_DEBUG",
      "UNICODE",
      "_UNICODE"
      ],
      "windowsSdkVersion": "10.0.19041.0",
      "compilerPath": "<<pathtoVS2019>/cl.exe",
      "cStandard": "c11",
      "cppStandard": "c++17",
      "intelliSenseMode": "windows-msvc-x64"
      }
      ],
      "version": 4
      }
    5. Go back to the command palette and select the command to edit the launch.json file. Then choose C++ Windows and finally the option for cl.exe. It should auto generate the launch.json and tasks.json files for you.
    6. launch.json is the settings used to launch the debugger. The tasks.json file is where tasks are stored including building.
    7. In the launch.json file it is configured to debug an exe that matches the current file name. Since your project probably has multiple source files you should change it to the name of the program instead. Note also that it includes a pretask to build the code first. The prelaunch task name must exactly match what is in tasks.json.
    8. Inside tasks.json is the compiler build commands. It by default only builds the current source file so you'll want to change that to build all files. Also ensure that the generated output file matches what the debugger wants to run.
    9. Once all the files are in place then simply pressing F5 should trigger a build and then start the debugger. Output should go in the integrated terminal but you can change that if you want.

    launch.json (in .vscode folder)

    {
       "version": "0.2.0",
       "configurations": [
          {
             "name": "cl.exe - Build and debug active file",
             "type": "cppvsdbg",
             "request": "launch",
             "program": "${fileDirname}\\test.exe",
             "args": [],
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",
             "environment": [],
             "console": "integratedTerminal",
             "preLaunchTask": "C/C++: cl.exe build active file"
          }
       ]
    }
    

    tasks.json (in .vscode folder)

    {
       "tasks": [
          {
             "type": "cppbuild",
             "label": "C/C++: cl.exe build active file",
             "command": "cl.exe",
             "args": [
                "/Zi",
                "/EHsc",
                "/nologo",
                "/Fe:",
                "${fileDirname}\\test.exe",
                "${fileDirname}\\*.c"
             ],
             "options": {
                "cwd": "${workspaceFolder}"
             },
             "problemMatcher": [
                "$msCompile"
             ],
             "group": {
                "kind": "build",
                "isDefault": true
             },
             "detail": "Task generated by Debugger."
          }
       ],
       "version": "2.0.0"
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.