Edit: Images for the article have been lost due to site transitions. Reading the instructions should be sufficient enough for tech savvy programmers to understand how to do it without the use of images.
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 explaining for newcomer’s to the world of programming.
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. Visual Studio Code mostly offers an environment where you can edit code in a light weight package. 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.
Okay so now let’s begin the actual process of upgrading Visual Studio Code. Note that this solution is going the Linux Bash/Windows 10(creators update) route but by the time you finish reading this tutorial you will realize any compiler can be used on top of Visual Studio Code. This is just a convenient route using tools already built into Windows 10. After reading this guide you’ll more than likely find a solution that better fits your needs.
- 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 further improves upon an already cool piece of software. The most notable extensions are the ones that allow Visual Studio Code to understand different types of programming languages. Some extensions even auto complete your in progress syntax.
- Microsoft offers the best tool that knows all of the C++ intricacies called Intellisense. In order to get this extension use 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. At this point 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 for the next step your going to need Windows 10. The reason why you need the latest version of Microsoft’s operating system is because, believe it or not, Microsoft has been slowly adopting and absorbing Linux. A lot of the main functions that make up Linux have been slowly rolled out to Windows 10. Soon even a Secure Shell will even be onboard.
- Step by step lets get the crucial parts that are required and hidden in Windows. First we need to get 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 Control Panel. If you don’t know where the Control Panel is just search for it using Cortana then hit the Programs section inside. Underneath Programs and Features hit Turn Windows Features On and Off. Then a smaller window should show up. Scroll to the bottom and hit the check box labeled 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. Unfortunately for us the Bash doesn’t know which type of Bash it should be. There are several different kinds that use different commands. For our use we will just look into the Ubunto styled bash which now shows up on the Windows Store. Hit the link for a direct landing page on the Microsoft Store Ubunto. When you install that open the app and enter a username and password. The regular bash should now be working without any errors and so does this new one. Lets continue with the previous bash we were using. Notice how you don’t have any errors this time when you open it.
- 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.
- 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.
- 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.