DEV Community

mikkel250
mikkel250

Posted on • Edited on

Setting up VSCode for C in linux

First, if you haven't yet, install the C/C++ extension that is available from Microsoft.

Also note that, since Macs use a *nix OS, most of this can be applied to setting up on a mac as well, with the possible exception of some of the paths.

I'd also recommend installing Code Runner as well - it's not as flexible as the setup described below, but it's really handy for lots of other languages. I just started using it for JavaScript and it's great to be able to run and test your files right in the editor without having use a browser.

Once the C/C++ extension is installed, you'll need to create a workspace to set up and run compile tasks. I'd recommend creating a top level parent folder to hold all your C projects and then use subfolders for each project (good if you're doing learning exercises). Another option would be to create a test folder to get comfortable with this setup, and then create your real project folders and set them up as their own workspaces, which will result in a more organized final setup.
Once the folder is created, go to File>Add Workspace to Workspace. Then save the workspace from the File menu, or, once you close the editor, you'll be prompted to save it. Once you're comfortable with setting up the compiler, you can also create a separate workspace for each project folder instead of using the top level parent, if you prefer to organize it that way.

Once done, use Ctrl + Shift + P to open the command pallette, type in "c/c++" and choose 'edit configuration UI' from the list. In the window that opens, scroll down and click on "Compiler Path" dropdown. Mine automatically detected the installed compilers and listed them in the dropdown, but if yours doesn't for some reason, you can always find the path and add it manually. Every linux system will generally ship with a C compiler, so you can search for yours via google, or in the terminal try the which command and the name of common compilers: gcc, clang, llvm. Then type the path into the "Compiler Path" field. Mine were in /usr/bin/clang and /usr/bin/gcc. Most likely yours are too.
If for some reason the user input field doesn't show up, you may have to click the "Add configuration" button in the section above. Just give it a name (like Linux default) and click OK and then the compiler path dropdown should (hopefully) appear.

Then scroll down to the "Intellisense mode" section and make sure that either ${default} is selected, or that the correct mode is selected to match the compiler and PC architecture (e.g. if using the clang compiler, set the Intellisense mode to clangx64 if you're using a 64 bit PC). If default is selected, it should pick the right one.

And that's it! Real easy. The settings should now be saved in a c_cpp_properties.json file.

Now, if you want to compile files, just use Ctrl + Shift + B or choose 'Run Build Task' from the Terminal menu at the top. This should run the compiler.

If you'd like to set up the file to both compile and run, I had to try this a few times to get it right, so here's how to do it:
Once you've entered all the info above, you should now have a default build task file that will tell the editor what to do when you use Ctrl + Shift + B. You can open it by going to Terminal>Configure default build task (or serach in the command palette). It will open the tasks.json file.
The data here should be filled in after setting up the configuration UI (it saves to this file when you make changes) but if it's not, make sure edit it so it appears similar to the one below:

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "type": "shell",
      "label": "gcc build active file",
      "command": "/usr/bin/gcc",
      "args": [
        "-g",
        "${file}",
        "-o",
        "${fileDirname}/${fileBasenameNoExtension}",
        "&&",
        "${fileDirname}/${fileBasenameNoExtension}"
      ],
      "options": {
        "cwd": "/usr/bin"
      },
      "problemMatcher": ["$gcc"],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Note that I am using the gcc compiler, so the arguments may differ if you're using a different compiler (Google is your friend here). Most linux systems ship with a gcc compiler, so conceivably you could just copy and paste the above into your file if you just want to get it done and get back to coding.

The important thing here is to make sure the command is correct, and that the arguments are filled in. To get the file to run right after the build, make sure to add the following to the end of the args list (what I struggled with was figuring out you needed to put the '&&' in as a separate argument):

"&&", 
"${fileDirname}/${fileBasenameNoExtension}"
Enter fullscreen mode Exit fullscreen mode

That should get you all set up to compile and run files directly in the editor, which makes testing as you go and debugging a lot easier.

As far as using Code Runner, you can use it to automatically compile and run as well using Alt + Ctrl + N, but I've found it to be a bit less flexible when it comes to C files. If you know the file should run without issues and just want to quickly see the output, this is a good option to use as well.

If you'd like to test it out really quick, you can copy and paste the code below into a file with the .c extension, save it, and then use Ctrl + Shift + B and make sure the text prints to your screen.

#include <stdio.h> 

int main() 
{
  printf("VSCode is set up correctly! Yay!\nHappy Coding!\n<3 Mikkel.");

return 0;
}
Enter fullscreen mode Exit fullscreen mode

EDIT: Since this post is getting a lot of attention, I added a little bit of clarification on workspaces, the test code above, and decided to add it to my series on learning C, as I think this will be helpful for those who are learning the language. Once it is possible to control the order of a series, I'll move it to the top (rn it's based on post date AFAICT).

Enjoy!

Top comments (0)