C++ Project setup VSCode

VSCode is a general purpose IDE whereas CLion is a niche IDE for C and Cpp.

To setup a C++ project in VSCode with the same capabilities that CLion has we’ll need to perform the following steps:

Search and install the C/C++ extension from Microsoft
image

Write your first cpp program:
image

Now if you want to build and run the program, you may use the play or debug symbol at the top right of the IDE:
image

The first thing you’ll face is a question on whether to use one compiler or the other. If you are using Apple Silicon you’ll probably want to use Clang++, if you are on a different platform, search which is the recommended compiler on that platform.

Either you use Run or Debug, the result will be the same, it will run it in the debug console.

Now there are two things to address:

  1. How to run the program outside the debug console? (so we can not only output data to the console but also insert values to the program)
  2. Enable the code linter by default (so we can spot errors earlier)

1. Run the program outside of the debug console

To run the program outside the debug console we can simply open a terminal and run the compiler and run commands like so:

Open a new terminal (Terminal → New Terminal), i’m using bash but you can use any other.
Run the command clang++ myProgramFileName. E.g. clang++ index.cpp

This will generate an output file, it’s a.out by default. Now we can run it through the same terminal like you’ll do with any .sh or similar, this is: ./a.out

Full picture:

It should be possible to automate this process but I believe that will do for the course, if you have a workaround to automate this integrated within VSCode feel free to add it in the comments.

2. Enable the linter

Now, if you started the course, you’ll see that when @Mosh tells us “here you’ll see a warning” on Fundamental Data Types -> 5- Narrowing video you simply won’t.

This is because you need to enable the linter. You can do it for the opened file, all files and so manually by using Command + Shift + P on mac or Ctrl+Shift+P on Windows and typing `>c++ analysis:
image

Now if you want to automate this (recommended) we can do it through VSCode Settings like so:
1- Go to settings:
code -> settings -> settings or search for Settings (UI)
image

2- Search for Clang-Tidy and enable it:

And that’s all for now, shall you have any question or recommendation I’m all ears :slight_smile:

Happy coding

2 Likes

Thanks for the instructions.

Just a common mistake is VS Code in not an IDE per se. It is a highly extensible Code Editor.
But the lines are easily blurred I agree.

Cheers.

Hi UniqueNospaceShort,

There’s no “official” definition for IDE so it depends on what you consider, subjectively, an IDE should be.

A common explanation is that a code editor allows you to edit code (period) while an IDE allows you for running tasks such build, debug and so on within the application while having complete support for evaluation (intellisense, linting) etc.

You can definitely run building, running and debugging tasks from within VSCode, it has integrated terminal, code linting capabilities, intellisense capabilities, code formatting capabilities, VCS integration, workspaces and so on and so forth, so I consider it an IDE and there’s nothing wrong with that.

Feel free to choose your preferred definition and considerations, tho. :slight_smile:

Hi,

there are indeed many subjects that are interpretted differently.

As for VS Code I simply go with the official description given by Microsoft themselves.

Also with the simple explanation that what distinguish IDE’s from code editors is that the tooling is integrated and thought to be used together from the ground up.

This article explains it very well.

I surely prefer having a way to clearly distinguish A from B.

Cheers

P.S. Red Hat definition of IDE hints for the definition given in the article.
Vs Code just is of the VIM, emacs kind of tool.