Join Bill Weinman for an in-depth discussion in this video Using Microsoft Visual Studio with the exercises, part of C++ Essential Training.
- To follow along with the exercises in this course, you'll need a development environment that includes a Modern C++ compiler that supports C++11 features. In this movie I'll show you how to use Microsoft Visual Studio for the exercises in this course. This machine is running Window 8.1 and I've installed the 2013 edition of Visual Studio Community, which is the latest version available as I record this course. So this is the Visualstudio.com website and if I click here on Downloads and Visual Studio Downloads, you'll see there's a number of choices, and these are the pay-for-it versions, these are free trials, you probably don't want one of those and these are the free versions.
I'm using the Community 2013 with Update 4 version. Now you don't have to use Visual Studio with this course. You may use any development environment you like, as long as it includes a conforming C++ compiler. There are usually several editions of Visual Studio available, and some of them will have what you need, and some of them won't. Visual Studio Community is a good choice if you don't have one of the paid editions. It has most of the features from the Professional Edition, and it's available free from Microsoft.
If you have one of the paid editions of Visual Studio that includes a Modern C++ compiler, you may, of course, use that instead. An older version of Microsoft C++ compiler may work for some of the exercises in this course. Just know that some exercises may not work with an older compiler. Microsoft Visual C++ Version 18 or later should have all of the features you need and I'll show you how to check the compiler version later in this movie.
So you can see here, I've copied the exercise files to my Desktop, and now I'm going to start up Visual Studio and I'm going to create a new project. Now, here I've selected General on the left side under Visual C++ and Empty Project and I'm gonna name my project Working and I'm going to put it in my, I have a folder set aside for this in Documents, under Documents and C++ Essential Training, C-P-P-E-S-S-T, and that's the folder I'm going to select and the solution name is also "Working." I check this Create Directory.
I don't need Source Control for this and I'm going to press OK. And this will create the project. I make sure that I select my Working project and I'm going to right-click on this and select Properties. There's just a couple of things we need to change here. Under C/C++, you'll see Preprocessor, and I need to set a couple of Preprocessor definitions. So I'm going to come over here and select this drop-down and Edit, and I'm going to add a couple of preprocessor values.
So the first one is underscore C-R-T underscore SECURE underscore NO underscore WARNINGS. And spelling really counts here. You're just going to need to get all of those letters exactly as they are and all in capital letters like that, otherwise this will not work. Then I'm just pressing the Return Key to get to the next line, and I'm going to add another one as well, and this one is underscore HAS underscore ITERATOR underscore DEBUGGING and the equals sign and the number zero.
Again, spelling is really critical here. Just take your time and make sure you get all of that exactly right. So the CRT Secure No Warnings, gets around a limitation with Microsoft C++. Normally, Microsoft C++ compiler will generate warnings when you use some C++ standard library functions. This course is designed to teach standard C++ and these functions are part of the standard and they're the standard way that you do some things.
Microsoft has their own versions of many of these standard functions and you may certainly use those if you prefer, but for the purpose of this course you'll need to be able to use the standard functions and this setting will allow you to do that without generating error messages. The Has Iterator Debugging Equals Zero, this gets around a sort of a bug, a sort of a misfeature in Microsoft's implementation of STL iterators, and this just makes it work like the iterators do on other compilers, and there's some controversy around how they're supposed to work and the standard isn't exactly specific on a couple of things.
But this makes it work the way that most other compilers work, and again, for your production code, you probably don't need either of these things, if you're writing code specifically for the Microsoft environment. If you're writing standard code or if you're compiling standard code that other people have written in other environments and you want it to work in Microsoft C++ then you may need these. But for the purpose of this course, we need both of these and so I've added them. I'm going to press OK here, and I'm going to press OK here, and now we're actually pretty much set up.
So we're going to test it. Now, I'm going to come back up here to my Working project, and I'm going to right-click and under Add, I'm going to select Existing Item, and you notice we have the shortcut available, Shift Alt A, and so if I get rid of these menus here, and I just press Shift Alt A, I get this Add Existing Item to the Working project dialog box. I'm going to come up here and I'm going to select my exercise files, which are on the Desktop and I'm going to come into Chapter One, and I'm going to grab Hello.cpp, and I'm going to Control Drag, and that just creates a copy, and so this has a working copy.
I just had to drag it a little bit and it creates a working copy and that's actually the one I'm going to select. You want to always work with a working copy, and not with the original one. That way, if something goes wrong and you want to revert back, it's really easy to do. So I click Add and now you'll notice under Source Files, I have Hello - Copy.cpp, and there it is in my Editor. Now just a quick note, I changed the colors on my Editor a little bit, just to make it a little bit more visible here, and I made my font bigger.
All of those things are available under Tools and Options, and Fonts and Colors. I made the foreground White and the background Black, and I made the size a little bigger and this is just so that it's readable on the screen here. Of course, you can configure yours, however, best works for you. Now the exercise files for this course are very simple C++ programs. This is a very standard Hello World C++ program right here.
They run without the Graphical User Interface, and the reason for this is that all of the code that supports a Graphical User Interface, tends to be a little bit complicated, and also tends to be very non-standard. Again, this course is about the language C++. It is not about how to get graphical user interfaces working in different environments, which are all by necessity very different. So all of these programs, in the exercise files, are Command Line programs. They run at the Command Line and this unfortunately is one thing that Microsoft Visual Studio is not designed to do very well.
So what I'm going to show you here is how you can run a Command Line program like all of these exercise files in Microsoft Visual Studio. This allows these programs to be very simple, very uncluttered and to focus specifically on the features and the language that we are talking about, and to have the code be cross-platform, so the same source code runs the same here, it runs the same on Unix, it runs the same on a Mac. It allows you to focus, just on what you're learning about the C++ language. The first thing I'm going to do is, is I'm going to build this, and under the Build Menu, you'll notice that Build Solution is F7, and so again, I'm going to get rid of that menu.
I'm just going to press F7 on my keyboard and I get this little build window and it says that it built it successfully. One succeeded, zero failed. It's a success. Now, what I need to do is a I need to test it. Visual Studio has no Command Line Console. So in order to test it, what I need to do is, is a I need to bring up a Command Window, and I've got one here in my Toolbar down there, and this brings up a Command Prompt.
But now I'm in this directory here and I'm not in the directory where I want this to be. That's really not the way that we want to run this here. Instead, I'm going to bring up my Explorer Window, and I'm going to navigate to where my Debug Folder is for my project. Now remember, I put it under My Documents C++ Essential Training and there's our Working project, and so I double-click on that, and there's the Debug Folder. I double-click on that and there's my Working.exe.
That's the code that I just built, right here, and you'll notice this path, Users Bill Weinman Documents C++ Essential Training Working Debug Working.exe. And so that's this right here. So you want to navigate to that. Obviously yours won't say Bill Weinman, it'll have whatever your user ID is on your machine, and you want to navigate to wherever it is that you built your project and find the Debug Folder underneath that and the Working.exe there. Now what I do is, when I'm in this Debug directory I come up here to the File Menu, and I just click on Open Command Line prompt and it opens a Command Line Prompt for me in that directory.
So if I type D-I-R, there's my Working.exe, and if I just type "Working" it runs it, and here it says "Hello World." So now every time we build something in Microsoft Visual Studio, just switch to that Command prompt that you already have open and type "Working" and it will run whatever that is. Then when we're done with a particular exercise, I can just delete it from here. So I highlight it. I press the delete key on my keyboard and then I come up to Build and I run Clean Solution.
Unfortunately, there's no shortcut key for that. Clean solution. And now you'll notice that when I come back out here to my Command Line prompt and I type D-I-R, there's no files there anymore. It has actually cleaned the solution and gotten rid of all the cruft in that directory that it uses to build its intermediate files and such. Now I can just bring in another source file, so Shift Alt A, and I want to bring in this one that says CPP11.cpp, and there it is.
Actually, no I want a working copy, so I'm going to delete that and I'm going to come back out here. Alt Shift A and I'm going to make a working copy of it. Control Drag and there's my working copy. Here's the source code to it. And what this one does is this just tests a couple of C++11 features, like the Initializer List, the AutoType and the new Range-based For Loop, and I'm going to build that, Press F7 and it builds it, and I come out here to my Command Prompt and I type Working and there's the result that I get.
I is one, I is two and that's correct. If we look at the source code here, we see that it's going through this loop and it's saying "I is" and printing it out for each one of those with a new line. This is now working great! I just want to do one more thing. I want to check the version of the compiler that comes with my version of Visual Studio. I can do that very easily. I type Print F quote M-S-C version is percent D, It's an integer value and we'll learn more about this obviously later in the course, a backslash and the letter N, and that gives us a new line.
Then I move past this quote and type a comma, and all caps underscore M-S-C underscore V-E-R, like that and a semicolon at the end of the line. What this will do is this will find the version of the compiler and it will display it on the console. I save with Control S, and I build with F7, and I come back out here to my Command Line prompt and I type "Working" and we see there's our one, two, three, four, five and it says MSC version is 1800.
Now the way this version number is constructed, the first two digits are the major version, and the second two digits are the minor version. So this is Microsoft C++ Version 18 and that is the minimum version that we want to be able to, if your version is higher than that, obviously, it's just fine. This is the version that supports the features that are described in this course. So that's all working great! And so I'm going to delete my working copy here, and I'm going to run Clean, and if I want to I can come back out here to my Explorer Window and I can come out here into the Exercise Files, that's on my Desktop, and I can actually delete my working copies here from there.
So now you have a working development environment for following the exercises in this course on Microsoft Windows using Microsoft Visual Studio.
- What is C++?
- Anatomy of a C++ program
- Writing statements and expressions
- Declaring variables
- Using loops
- Defining functions
- Getting the most out of the preprocessor
- Creating classes and objects
- Undertanding data types
- Overloading operators
- Understanding inheritance
- C++ template programming and the STL
- Handling exceptions