Join Bill Weinman for an in-depth discussion in this video Using Xcode with the exercises, part of C++ Essential Training.
- In this movie, I'll show you how to create a workspace in Xcode for following along with the exercises in this course on a Mac. Any version of Xcode from version five to the current version should work fine for this course. I'm using Xcode 6.1 with Mac OS X Yosemite. Xcode is free on the Mac app store, it requires a modern Intel-based Mac running a recent version of OS X. I've already installed Xcode on this Mac and I've placed the exercise files on the Desktop.
So, I'll start by firing up Xcode and from the File menu, I select New and Workspace. I have a folder called CppEssT under my Documents directory, I've already created that folder. I'm going to name this CppWorkspace and I'll press Save. So, now I have an empty workspace and I need to add some files to it. So, I'm gonna select the plus down here in the lower lefthand corner and add files to our workspace.
My exercise files are on the Desktop and so I want to select that folder, and that, of course, has all of my exercise files in it. But I want to just select this ExerciseFiles folder, the whole thing with all the subfolders in it. You want to make sure that Copy Items is not checked, so just like this is fine. Create folder references is fine, and I press Add. And, now I have a reference to my exercise folders. It didn't actually copy the exercise folders, this is simply a reference to the exercise folders on the Desktop.
Now I need to create a project for working with the exercise files. This is not a project and Xcode will not compile these files directly from here. So, here in Chapter One, we have these files, but Xcode will not compile that, it needs a project. And, if I press this plus, you'll notice that there's nothing down here that says Add Project. So, what I need to do is I need to right-click anywhere in the empty space here on the left-hand side, or control-click, I'm gonna press control and click my mouse button.
And down here it says New Project. It will create a project and it will add it to our workspace, which is what we want. So, new project, under OS X, select Application and Command Line Tool. All of these exercise files are designed to run just as a Command Line Tool. That way we're focusing on the features of C++ and not on anything that has to do with a particular operating system. We're just learning about the language, we're not going into how to create applications that will run on a Mac or will run on Windows, we're just learning about the language itself, and a Command Line Tool is the best way to do that.
So, we're gonna create a Command Line Tool, I'm gonna select Next, and I'm just gonna name it Working, and Organization Name, Identifier, Bundle Identifier, none of that's actually used. So, I can just leave that alone. The language should be selected as C++. And I'm gonna press Next. We do not need a Git repository, won't hurt, but we're not gonna use it, so I'm just gonna leave that alone. I'm gonna add it to the workspace and the group of the workspace, also.
So, I just select Create, it'll put it right here in this same folder, our Documents CppEsst folder. And I'll select Create, and now we have a working project. So, inside this working project it has a working group, which is fine. And it has this main.cpp, which we're not actually going to use, so I'm gonna go ahead and delete that. I'm pressing the Delete key and I click Move to Trash, so that's now gone.
And I'm just gonna drag this hello.cpp up here into my project and I want to drag it into the Working group here, and you'll notice that depending on how I move my mouse, that little circle can either go left or right. So, I'm dragging that file into that folder, and I want to make sure that Copy is checked for this, and Add to targets: Working is checked, and I just click Finish. And, now when I select this I've got my code there.
I'm gonna organize my screen a little bit, just because I have such limited real estate here. And this'll work great for what we're doing. For your purposes, you probably have a lot more screen real estate and you probably don't need to organize it the way that I do. But this way I've got the maximum amount of space for my code and a little bit of space for my console down here which is where the results of the code is going to be displayed. And so, at this point, I can just press this run button here, this will build and run in the current scheme, or I can press cmd + b on my keyboard, which will just build it.
Or, I can press cmd + r on my keyboard, which will do a build and run. I'm usually just gonna press cmd + r on my keyboard. I'm a keyboard guy, my fingers are usually on the keyboard and it's just the most convenient way for me to do it. You can do it however it works best for you. So, this is compiled and run our little Hello, World program. And you see down here in the console, it displays the results, "Hello, World!" and a little message that the program ended with the exit code: 0. We'll get into that a little bit later, what that means.
But for now, notice that the results are always in bold down here in the console. And that's what we're looking for to show that the program is running just fine. The purpose of a Hello, World program is to test your environment, it's to get your development cycle going, to make sure that everything is working just the way that you want it. And if you make some changes in your code, like, for instance, instead of World, I can make it say, "Hello, Bill!" and I can save it, I'm pressing cmd + s on my Mac keyboard, and run it pressing cmd + r, and you see, now it says, "Hello, Bill!" So, I have a working development cycle.
I can make changes to my code and my code will run in my environment and so I can test it. Now, as we go through this course, we're sometimes going to need to change the file that's here, we can only have one of these files in our project at a time. The reason for that is because there's this main function and you can only have one of those in a C++ program at a time. That's the main entry point as defined in the language. So, if I were to put another file in there, with another main, it would create a error and it wouldn't compile and run.
And so, instead, what I'm gonna do is I'm gonna delete this, let's say we're in an exercise and we're going to change to a different example file, so I'm gonna delete this, I highlight it and I press the Delete key on my keyboard, and I select Move to Trash. And then I grab another one of these, let's say cpp11.cpp and I drag it into my Working folder, I make sure that Copy items is selected and the target is selected, that shouldn't change between actions here.
I select Finish, I highlight the file inside the working folder there, and now I have my new C++ file and I can just press cmd + r and it'll build and run it and I get my results. The purpose of this file, this cpp11.cpp file, is to test some modern C++ 11 features that don't exist in older versions of C++, that are only in this current version of C++. So, we can make sure that we have a modern compiler that will compile the examples for this course.
So, this one it uses an initializer list, that's a new feature, it uses the autotype, that's a new feature, and the range-based for loop, which is a new feature. And all of these things are running, if we get this result, "1, 2, 3, 4, 5" like that. So, at the end of each lesson, we're going to delete whatever is in our working project here. So, I'm gonna highlight it and press the Delete key, and select Move to Trash, and then we're going to run Clean, now there's a couple of ways to do this. I tend to use the keyboard, but from the mouse menu, you can select Product and Clean, and you see it says shift + cmd + k.
So, if I press Clean, it says Clean Succeeded. Or, I can press shift + cmd + k, and I get exactly the same result. What Clean does is it gets rid of the executable and all the intermediate files from the Product. Intermediate files are created by the compiler whenever it compiles something, and if the date and time stamps on those files aren't exactly right, it can confused the next time it tries to compile something. So, I will always delete and run Clean at the end of the exercise files.
So, now we have a working Xcode workspace, for following along with the exercises in this course. Xcode is an excellent IDE, and Clang, the compiler that comes with Xcode, is a superb compiler. This combination provides a great environment for this course.
- 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
Skill Level Intermediate
Q: How can I follow the exercises on Windows without paying for Microsoft Visual Studio?
Q: I stuck in the section on setting up Visual Studio 2015 for use with the exercise files. Where is the "C/C++" section in the Property dialog?
A: There's a bug in Visual Studio 2015 which prevents the C/C++ section
from appearing in an Empty project. In order to get to this section you
will need to first add a C++ file to the project. Here's how to do this:
1. Press Shift-Alt-A to "Add an existing file" to the project.
2. Select the "hello.cpp" file from the Chap01 folder of the Exercise Files and press the "Add" button.
3. Confirm that "hello.cpp" is listed under Source Files, then right-click on the "Working" project name and select "Properties".
4. Now you should be able to select the C/C++ section and proceed with the installation video instructions.