Getting Started on macOS or Linux with VS Code

Start course
Overview
Difficulty
Beginner
Duration
40m
Students
45
Ratings
5/5
starstarstarstarstar
Description

This course will help you prepare for the Complete C++ Developer learning path and get you set up with the right development environments.

Learning Objectives

  • Set up Visual Studio, VS Code, or Code::Blocks depending on whether you'll be working on Windows, Mac, or Linux as you follow along with the exercises in this course

Intended Audience

  • Beginner coders, new to C++
  • Developers looking to upskill by adding C++ to their CV
  • College students and anyone studying C++

Prerequisites

This is a beginner-level course and so no prior knowledge of C++ is necessary.

Transcript

If you are working on a Mac or Linux computer, then I would also recommend Visual Studio Code as the IDE. Especially for newer versions of Mac, the code blocks does not actually work yet. There's also a Windows version of Visual Studio Code as well, so you can use this if you prefer it over Visual Studio IDE. There are certainly other options. For example, if you already do development on Mac, you might like XCode and you are perfectly welcome to continue using that. This shouldn't be confused with Visual Studio IDE. It is a different piece of software altogether. But with Visual Studio Code, you can create and write programs using C++ once you have it set up with the correct extensions and know the proper way to use it. Visual Studio IDE as a post Visual Studio Code which we're discussing now is available for MacOS but it does not officially support C++. So, I don't recommend it. So, as mentioned, let's look at how we can use Visual Studio Code for C++ development on Mac or even Linux. First, we have to download it. So, you'll see that I went to code.visualstudio.com or you can do a Google search for Visual Studio Code or VS Code and you'll notice that there are downloads available. There are for all three platforms OS, MacOS, Windows and Linux, a couple different for Linux. And right here, since it detected I was on a Mac, I click that and it takes me to a Getting Started page and I do get a zip file down here. I'm going to show it in finder right here and there's an executable program right out of the box, so to speak, available on MacOS. So, we just need to unzip this which is simple on a Mac and requires a double click. So, I'm just going to double click this, give it a couple of seconds, and there's Visual Studio Code. I will drag it over onto the desktop. You could put it in your applications or wherever you feel comfortable. So, let me close that, there's another file open we'll look at in a minute. So, we want to create a folder to house the projects that we're going to be working on. So, just like we would with Visual Studio IDE or any other IDE, it's good to have a top level folders. Alright, so cpp-workdir for working directory, and there you go. So, let's run Visual Studio Code by double clicking it. So, we will tell it to open it. The concepts of projects are going to be different with Visual Studio Code than it will be with Visual Studio IDE. So, actually let's not make this the full size, I'll just take that over there and make it a little bit bigger. There we go. So, again, projects will be different for VS Code than what you'll be seeing me do with Visual Studio IDE on Windows throughout the course. Visual Studio IDE usually has a top level folder for a so-called solution and then inside the solution you have one or more projects. It also creates other sub folders for source files where the code is written and the compiled objects that the computer can understand. VS Code is more of a folder-based code editor. We will create folders and store our code in there. So, let's create an empty folder to house our project. So, from inside of Visual Studio Code, VS Code, I'm going to click this little Icon up here, looks like two papers, documents, and you can actually say open folder. And if I go to the desktop inside of cpp-workdir, I could open the cpp-workdir directly if I want to and that would be fine, and you can create folders underneath. If you want to keep all of your projects available or you could just select a particular individual folder. So, for example, I could go to now file close folder and if I want to I can now click there, open folder and then single click there, and then inside here I could select any subfolders that I might have. So, let's create inside using Finder. One way to do this would be to right click new folder and we could call this one test-1-vscode, alright. And now I'll go back to open folder and inside cpp-workdir, I have the test-1-vscode. So, I'll just use this one. It opens it up and it's an empty folder. You'll notice that there's a little arrow here that you can expand and collapse but there's nothing inside of it. So, now we need a source file which just as in the videos that you will see, we will name main.cpp. That is what I name essentially all the files so even though the layout and interface are different in Visual Studio IDE from Visual Studio Code, you should be able to follow along quite nicely using Visual Studio Code. There are a couple of different steps but it shouldn't be too difficult to get up and running and for you to get used to them. So, let's write some simple code. So, I'm going to first click this little button to create a file and we'll call it main.cpp for C++, hit 'Enter' and it's just a little file here. It's actually asking if we want to install the C++ extension, we'll get to that. Now, I'm going to write the code include <io iostream using name space standard. Okay, and hello world. Here we go. So, the code may not make much sense to you yet but don't worry. This is just a test to make sure that you have everything installed. As I mentioned, there are a couple different steps from what we'll do in Visual Studio IDE in the actual lectures. So, let's perform these steps to build and run our application. The first thing is as it's recommending down here, it says the C++ extension is recommended and you're going to click install. Now, if you don't get this message down in the corner, you want to go to the extensions option here and type C++ and you'll notice it'll pop up right here and ask if you want to install it. So, either way down here or over here we're going to click install. So, once it's installed, we should be good, good to go. I'll go back right here to the Explorer and we've also got main.cpp. So, you know it, you'll know the C++ extension is installed because it says uninstall override. Alright. So, next thing we're going to do is we have to go to the, and this is what you'll have to do with each project, now the extension is installed now. So, you don't have to worry about that. But this part that we're about to do, you'll have to do multiple times throughout the course. So, terminal. And you're going to go to Configure Default Build Task at the bottom, and then a bunch of stuff will come up here and you'll notice that there is C/C++: clang build active file, C/C++: clang++ build active file. I'm not sure why there's two of them there, but you should just select the first one that says build active file, just click it and really the only purpose for that is it will create a folder called .vscode and then this file called tasks.json. Now you don't have to really understand anything in here. If you're familiar with JavaScript Object Notation, it's kind of based on JavaScript but it's used for holding data and configuration and stuff like that and it looks really confusing and complicated. It's not really that bad but we're not really going to have to learn it for this course, and most developers that deal with this situation, copy and paste it anyway for most part. So, what we're going to do with this tasks.json file, we're going to replace the contents of it with the tasks.json I have on the course GitHub under the VS Code folder. So, once you've done this, you'll have the file contents locally and could just copy it into each of your new projects. So, I happen to have my GitHub already open and you'll notice if I go to my primary GitHub page here, it looks like this. Now if you have downloaded the code since I have updated this. So, it has been updated today. vscode and then tasks.json is right here and I usually just click row because I don't like, you don't need the formatting right now. So, I'll click row and it brings it up in here just in a browser. So, I'm going to copy all of this and it is, right click, copy and then go back into Visual Studio Code. And you're going to delete all of this stuff in the tasks.json and command V on Mac or you can do right click paste, whichever is better for you. And this has actually been updated from the default one that they offer through the Microsoft instructions. So, this will for sure work on Mac. This may still actually work on Linux as well with some, maybe some tweaks. So, this right here, {workspaceFolder}/*.cpp this is telling us that any CPP files we have in our workspace folder, our project folder are going to be compiled. So, when we start getting into classes and stuff, you don't have to worry about having to change this file. So, I would just keep using this file right here, every time you make a new project you just copy the contents of this file over. So, now with tasks.json, I'll save this little dot next to it because it hasn't been saved, I'm going to either go to file save or you can do command S and you notice the little dot goes away because now it's saved. Now with main.cpp selected, again with this code, that probably doesn't make sense yet and that's totally fine. We're going to actually run the build tasks. So, now that it knows what to do by tasks.json, we're going to tell it to run it and to build it rather. So, terminal run build task and it brings up a terminal at the bottom right here and it says what's happened, the debug console tells us what's going on and all this stuff, should be okay. So, one thing that can be a little intimidating for some new students is that with Visual Studio Code you will use some console commands which are text-based, instead of just interacting with menus and buttons, we've been clicking around all over the place and doing stuff like that, but the old school way to do it and actually it's not outdated because people do it all the time, and actually some developers find it quicker. That's why Visual Studio Code is so popular because it has a lot of console-based stuff built in and a lot of developers really enjoy it. Now you'll find, once you learn just a couple of commands, everything is very, very simple and you don't have to sweat it. But learning some of these basic commands is actually extremely exciting and it helps you to become an even more knowledgeable software developer in general and computer user as well. So, for our purposes, we'll usually only need to know a couple of very simple commands. So, right now it has the clang or clang window open from where we did the build but you're going to click either the arrow and then say you could do default shell, but let's just hit the plus symbol for new terminal. You could also go up to terminal, new terminal either way or use the key combination. I'm just going to click the little plus symbol there. So, we do that and you'll notice it has a name of the machine and user I'm on,  and one of the commands you need to know is ls for list and it's just going to list the contents of the directory. If you're familiar with Windows dir for directory, does approximately the same thing. But ls is what we're going to do to say what's in this folder that I'm in. So, it tells me I am in this folder, test-1-vscode and that is in fact the name of the folder, right? That we created. So, ls tells me there's this information for Visual Studio and then you've got a cpp file for main and you also have just this thing called main. Alright. So, the main without any extension on it is actually the executable that we created when we just built it. So, what you're going to do to run it, you're going to do . which means current directory / which means go inside of that current directory and we're going to do something, and then you just type main, don't type main.cpp, don't type main.dsym or anything else, just .main, and then hit 'Enter', and there you go, there's your output. So, when you see me doing the tutorials and I do start without debugging and things like that, all you really need to do most of the time is just go through the process of building like we just did and running like we just did, immediately. And you should be able to see the output just like we had here. So, as you can see we get the expected output 'Hello world!', this tells me I've got the stuff installed correctly, so that pretty much wraps up this basic installation and setup. I hope you enjoy the course.

 

About the Author
Students
297
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics