DEV Community

ACharLuk
ACharLuk

Posted on

Developing C++ with Visual Studio Code

Welcome to my first post! I will be showing you how I develop C++ using Visual Studio Code along with the C++ and Easy C++ Projects extensions.

Downloading Visual Studio Code

First we will need a code editor or IDE, I personally like Visual Studio Code (VSC for short) because it is lightweight like Sublime, but has some of the features I like from IDEs, specially Intellisense. You can download Visual Studio Code here, you will land on a page like this:

Click the download button and wait a few seconds for the download to start.


Setting up VSC for C++ development

The first time you open VSC you will see a welcome window. VSC has a very simple layout: a bar on the left with 5 buttons (File explorer, Find, Git integration, Debug, Extensions), a status bar on the bottom and a window with tabs for the editors. Click the last button to open Extensions:

To develop C++ we will install two extensions, the first one is C/C++, which is already shown in the last figure, to install it just click the green button that says Install:

Then we will search for "easy c++" and then install the extension called "Easy C++ Projects"


Environment setup finished

After installing the extensions, a blue button will appear saying Reload, clicking it will reload the window and activate the extensions we just installed, as shown here:

Good job! Now we have an environment for depeloping our first C++ project!


Creating our first C++ project with Visual Studio Code!

After setting up VSC for C++ development, we just need to create a new project! This is done in a few steps:

Creating a folder for the project

To set the folder for the project, go to File > Open folder (or press [Ctrl+K Ctrl+O]), a dialog like this will pop up:

Create a new folder and press "Select folder", the dialog will close and the window will reload and open the folder we have created.


Creating a C++ project

Press F1, a little window will appear with lots of different commands, search for "c++" and click "Create new C++ project":

Now we will be asked what compiler we want to use, look the Easy C++ extension page for information on how to setup a compiler for your operating system. I will use Windows Subsystem for Linux.


Code!

Now that we finished the creation step, VSC will generate the project structure for us, and open main.cpp with some example code:

To test that the project works press the Build & Run button on the status bar, a terminal will open and it will compile and execute our program for the first time!

Top comments (21)

Collapse
 
larssonted profile image
Ted Larsson

I´m a complete beginner at coding and is looking for a nice and simple IDE to use, I´m currently using ms visual studio but i want to find a more lightweight IDE for storage reasons. I tried vscode a bit, i managed to install a compiler but then all of a sudden i needed to edit json files and such things and i have no idea how to do that. I looked at youtube videos but learning all that AND c++ was too much information at once. I´m learning to program in c++ at the moment and will be moving on to c# in a few weeks, does this method make it easier to compile and debug projects? or is there another IDE you can recommend?

Collapse
 
acharluk profile image
ACharLuk

If you know how to install your compiler it is much faster to develop with VSCode and this extension imo, it has intellisense and automatic project creation!

Collapse
 
larssonted profile image
Ted Larsson

I've managed to install the compiler but when it comes to configuring .json files and such things I'm lost.

Thread Thread
 
acharluk profile image
ACharLuk

If you create the project using Easy C++ you don't need to set up any json file yourself, you press F1, search for Easy C++ create new project and it will set everything up. A couple of buttons will show up at the bottom bar for building and running the project.

Thread Thread
 
larssonted profile image
Ted Larsson

Allright I'll look in to it 😊👍🏻

Collapse
 
arby3k profile image
Arbab Ahmed

I can't seem to figure out why I keep getting the following error when trying to run just the "hello world" project Easy C++ makes.

"The terminal process terminated with exit code: 1
Terminal will be reused by tasks, press any key to close it."

Collapse
 
pianocomposer321 profile image
pianocomposer321

I think that's because the template that Easy C++ Projects makes doesn't include a "return 0" statement, so the shell never knows that it exited successfully. I prefer C/C++ Project Generator to Easy C++ Projects, and would reccomend trying that.

Collapse
 
abhisingh127 profile image
abhisingh127

I get the same error

Collapse
 
abhisingh127 profile image
abhisingh127

can someone help me please?
whenever I click at build and run, It shows error like permission denied and then says error at int main()

Collapse
 
acharluk profile image
ACharLuk

Hey! Can you give us some context? What OS are you using? Are the folder permissions correct?

Collapse
 
abhisingh127 profile image
abhisingh127

Actually it was a problem with permissions. I am using Ubuntu 19.04 . The executable permission was not given.

Thread Thread
 
acharluk profile image
ACharLuk

I'm glad you got it solved! I'll take note of this to see if it is isolated to Ubuntu 19.04 or I should upload a fix, thank you for the info!

Thread Thread
 
abhisingh127 profile image
abhisingh127

Actually. It was due to my own ignorance. I forgot about the file permissions in linux. All you need to do is add the executable permission using the chmod +x command.

Collapse
 
goodmansoul profile image
GoodManSoul • Edited

This is not a complete guide to begin code with c++ language. Sad but true. Default C/C++ extension doesn't provide full compilation support. Needs gcc compiler...

  • not all libraries will be supported by simple gcc config (like "thread" library ...). For those who have an error -> download MinGW and add it to the PATH. +Have a good time
Collapse
 
txai profile image
Txai

Great post! I was looking for a substitute for eclipse-cdt. Do you think vscode can be a good comperitor to eclipse?

Collapse
 
acharluk profile image
ACharLuk

I was an Eclipse user and never went back when I changed to VS Code!

Collapse
 
ash1eyish profile image
Ashley Maria

I've used both and personally prefer VS Code.

Collapse
 
pianocomposer321 profile image
pianocomposer321

Personally, I prefer "C/C++ Project Generator" to "Easy C++ Projects". It works without asking you confusing questions about the platform. Also, I really like "Project Templates", because you can start with the basic template from the project generator, then tweak the Makefile and includes to make a more customized template. I currently have one for SFML, because it would be a pain to have to set it up every time.

Collapse
 
d3xsky profile image
Dejan

Thanks, worked like a charm!

Collapse
 
dalerr96 profile image
DaLeR

Thanks for the post, worked perfectly on Windows 10

Collapse
 
jacobgeogeek profile image
Jacob C.

I was wondering if its is possible to debug in VS code by using the WSL (with g++ or gcc install) ?