This article is meant to help people who want to build C++ programs using the text editor Visual Studio Code. After searching various Stack Overflow pages, YouTube tutorials, and more I gave up and decided to tackle that solution on my own. TICGN isn’t exactly a programming website and am sure some readers of this article are far and above from being a newbie so be aware I’ll be writing in an easy to understand manner and explaining some key terms.
First off you need to understand what exactly we are doing here. I am trying to turn the regular text editor(think notepad) Visual Studio Code into an all in one place to program. I want to write and test code in Visual Studio Code but unfortunately it’s just a text editor, no different than the program notepad. In order for it to become an all in one solution(Integrated Development environment) for my programming needs several things need to be installed. If you’re new to programming I suggest maybe starting off with regular old Visual Studio which is already an all in one solution. The reason why I am going through all of these hurdles is because Visual Studio Code is lightweight and faster.
Okay so now let’s begin the actual process of upgrading Visual Studio Code so that it’s as close to an integrated development environment as possible. Note that this solution is going the Linux Bash/Windows 10(creators update) route but can easily be adjusted to use the regular Visual Studio compiler(software that builds the code) inside Visual Studio Code. If you’re not a novice you can most likely take what is written here and find a solution that works for you.
- First download Visual Studio Code if you haven’t yet. Visual Studio Code is a piece of software that primarily lets you write text. It also has the ability to install extensions which basically make it understand the grammar of coding. One you install it and open it up we will be able to install the grammar for the programming language C++ itself.
- Microsoft offers the best tool that knows all of the C++ intricacies called Intellisense. Intellisense will help you out by letting you know if you wrote something wrong and will underline it in red or offer suggestions to complete your code. In order to download it you have to open up Visual Studio Code and then go to the left side bar. The icon below the bug labeled extensions when you hover over it is where we will get Intellisense. After clicking the extensions button a search bar will appear. Type on the bar C++. The first option that comes up should be Microsoft’s Intellisense. Click on it and download the extension.
- After Downloading it you should be able to debug(find errors in your program) which is all Visual Studio Code was meant to do. To be honest just debugging in Visual Studio Code is still complicated, who thought it was a good idea to need to write json files to debug? Alright forget the last sentence I wrote because we will never get into that territory ever again. Moving on, you still can’t actually build your program or see its output. The next few steps are going to further expand on Visual Studio so that you can compile(put together) a program .exe or a.out in our case.
- Next you’re going to need Windows. It’s not necessarily needed if you’re tech savvy and have a Linux bash for Windows you can grab a hold of but Windows 10 makes this step a lot more easier by offering their own solution. Step by step lets get the crucial part to building programs, the hidden Linux Bash on Windows 10. Go to the Windows Settings->Update & Security -> For Developers tab-> and then activate Developer mode.
- For the actual activation of the Linux Bash we will need to go to the Command Prompt. Use Cortana if you can’t find the Control Panel. When you get there hit the Programs section. Then underneath Programs and Features hit Turn Windows Features On and Off. Then a smaller window will show up. Scroll to the bottom and hit box for Windows Subsystem for Linux. Special thanks to HowtoGeek on how to activate the Bash.
- Windows might need to reboot but when you return you should be close to completing the project. Now when you go on Cortana and type Bash you should see the a program ready to be launched on a click. The thing is we just have the Bash itself but with the Linux subsystem activated we should be able to get Linux software which was hidden on the Windows Store until you activated it. On the Windows Store go and find Ubunto. When you install that open the app and enter a username and password. The regular bash should now be working without any errors so lets go back to that one.
- Now that we have this all set up we need one more thing. This entire process has been building the blocks in order to get a compiler, the software that builds the program from the text we write on Visual Studio Code. We needed the Linux subsystem and Ubunto because the bash offers something that regular Windows and the regular Visual Studio compiler don’t offer which is having the ability to open and use programs in a terminal. The main benefit of the Bash for us is that we will be able to open a compiler on it and build and test programs on it. So lets download the last piece of the puzzle the compiler.
- In order to download it we need to open the Bash and type in the following sudo apt-get update and hit enter. That will get the list of the latest versions of Linux software available to download. Next we will download the C++ compiler named g++ using this line in the bash sudo apt-get install g++ and hit enter. You should be able to see it downloading. The screenshot below may look a little different from yours because I set my starting path somewhere different.
- Alright so we finally have all the pieces. We have a text editor that understands the C++ language with Visual Studio Code. We have a bash that can build C++ programs inside of it using the compiler g++. Now how do we combine these things? Well one of the cool things about Visual Studio Code is that you can insert a terminal and integrate it directly inside Visual Studio Code. In our case, we will be inserting our Bash that has a C++ compiler in it. First open up Visual Studio Code on the top tool bar hit File -> Preferences -> Settings. In the settings you’ll have a split screen with the left side dictating the default settings of Visual Studio Code and the right side the overrides. You’re going to want to override the default terminal Visual Studio uses which is Powershell. In order to do that on the right side enter “terminal.integrated.shell.windows”: with the parenthesis and after that you’ll want to enter the path of the terminal you want to replace it with. If you don’t know the path of our Bash just type Bash in the Cortana search bar right click on it and hit open file location. This will open the location of where Bash.exe is at. Just copy the address on top by right clicking the address bar and copying the address. In my case it’s C:\Windows\System32 now everywhere you see a \ add another \ so that it looks like. C:\\Windows\\System32 add one more pair and put bash.exe C:\\Windows\\System32\\bash.exe now you have the address of where bash.exe is put it next to the terminal setting like so “terminal.integrated.shell.windows”: “C:\\Windows\\System32\\bash.exe” After writing that into the settings on the right hit ctrl s to save. You can now exit out of that tab.
Note that I have another override on top of the terminal. That one was automatically placed there by another extension I downloaded which changes the color of my Visual Studio Code. So don’t worry if it doesn’t look the same as yours.
- That’s all there is to it. You now have a bash that can compile in your Visual Studio Code. If you’re programmer or Linux savvy then you know how to move around in the bash and don’t really need to read the rest. For every one else I’ll write a “hello world” program with you.
- So let’s write our first program in C++ using our new tools. Close and Open Visual Studio. Starting from the Welcome Screen we will Open Folder and start our project in the desktop. So hit the Open Folder option and navigate to your Desktop and hit Open Folder. On the left side where Desktop is right click and hit new file. You’re screen should look like this.
- Right click on your new file and rename it main.cpp that will make it so Visual Studio recognizes it as a C++ file and will use intellisense to show you your “grammar” mistakes. Type in the code I placed on the screen below and hit ctrl S in order to save the file. Every time you want to test your code you should ctrl S to save it.
- Alright so our code is complete, time to open the terminal. Right click on the left side bar beneath you main.cpp preferably and empty area. Hit open in Terminal. That should open up the Bash we had before but inside Visual Studio Code. Now check out where the Bash starts from. You’ll notice it has a path already written showing you where in your folders the Bash is currently looking at. If it’s in the desktop then you’re fine. If your bash is not in the desktop type cd and the following while replacing the specific portion with the user name your computer has when you open your files.
My terminal doesn’t start at the desktop. So I typed cd /mnt/c/Users/username/Desktop$
If you want to know what cd does or other Linux bash commands here is a short run down. cd is the command to move around your files. cd .. will move up the file explorer. cd nameoffolder will move in to that folder or path. In our case we typed in cd and a path to go to the desktop. To know you’re in the correct place hit ls. That will show you all the files in the current location. We should see main.cpp listed after you typed ls along with other files in the directory. If you see main.cpp then good job, time to build the program. In the terminal type in g++ main.cpp this will compile your cpp file and turn it into an executable or regular program you can launch. Instead of spitting out a .exe Linux likes the .out file format. Don’t worry there’s literally nothing different between the two file formats. So you should see a file titled a.out show up on your desktop after you compiled your program. If it didn’t compile it means you did not copy the code exactly and mostly likely got an error. Going back to the a.out you can launch your new program by typing ./a.out inside the terminal. This will open the program. Since our program just types out Hello World that’s what you should see. Congratulations, you’re now a C++ programmer and have created the first program every programmer makes when they start their journey into programming. If you want to continue programming I suggest picking up a good C++ book or following youtube tutorials from TheNewBoston on youtube. Soon you’ll be making infinite loops and pulling out your hair with pointers too.
If you want to use C++11 type -std=c++11 in the terminal as you are compiling. Ex. g++ -std=c++11 main.cpp
You can use the regular Visual Studio compiler in Visual Studio Code. Just find the path of the compiler which is called cl.exe in the Visual Studio program files. Unfortunately you can’t enter it into the integrated terminal but you can set it as the external terminal but where is the grace in that?
If you have any questions leave them below.