Join Bill Weinman for an in-depth discussion in this video Using Xcode with the exercises, part of C++ Essential Training.
- [Instructor] 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. Xcode uses the Clang C++ compiler, which is part of the LLVM Project. LLVM is a modern compiler system and Clang is one of the most complete C++ compilers available. Xcode is free on the Mac App Store. It requires a modern Intel-based Mac running a recent version of OS 10.
I've already installed Xcode on this Mac and I've placed the exercise files on the desktop. I'm using Xcode Version 10.0 running on macOS Mojave, which is the latest versions of Xcode and MacOS available at the time of this recording, which is really just a few weeks after Mojave was released. We'll start by creating a new workspace, so I'll come up here to File, New, and Workspace.
I'm going to place the workspace in my Documents folder and I'm going to name it CPPWorkspace, like that. I don't know if plus signs work or don't work on this file system, but I do know that there are some circumstances where they may create problems, so I always just use P's for file names in place of the pluses. So CPP is C++, Workspace. And this is in my Documents folder, which is off of my Home directory there and I'll click Save and that creates the workspace.
You'll notice over here it says No Files and our workspace says CPPWorkspace Ready. So we're going to go ahead and add Files, actually folder references but... Over here to the plus and say Add Files, CPPWorkspace, and I'm going to select the Desktop and Exercise Files so this is the Exercise Files I have on my Desktop. We're going to uncheck the Copy items and leave the Create folder references and this will create references to my Exercise Files on the Desktop.
If I wanted to work with copies instead, I could select the Copy items instead, but as you'll see in the way that we use this, it's not really necessary because each time we use a file we'll be making a copy anyway. So I'm going to select Add and this adds this folder reference, the Exercise Files to my workspace. Now I'm going to create a new project for the working project so that we can actually compile and run the program.
So I'm going to come to File and New and Project. Now under macOS, I select Command Line Tool and Next. And I'm going to name it Working like that and make sure that C++ is selected here. It sometimes defaults to Swift. It says C++ here 'cause I've been using C++ on this computer. You want to make sure that C++ is selected as the primary language and select Next and then where we're going to put our project, I'm going to put it in the Documents folder again so I select Documents, I do not need a Git repository so I'll uncheck that if it's checked and I want to make sure that it's added to my CPPWorkspace and so that'll automatically populate the CPP group here.
I don't want to select any of those other folders. I want it in the workspace itself. So add it to the workspace and Group should also say workspace. Now, you don't need to save these files in documents if you don't want to you can save them wherever you want to, as long as you know where you've saved them and you keep track of that. So I'm going to say Create. And now we have a project in our workspace. We have the workspace we have the Exercise Files, and we have the project. I'm going to delete this default main.cpp file because we don't need that and it'll get in the way of what we're actually doing so I'm going to come up here where I can select Delete here or I can just use Command Delete on my keyboard, which is what I tend to do, so Command Delete and I want to say Move to Trash, but a shortcut is if I say Command Delete again, it does that for me.
Now, under Chapter two, notice we have these CPP files. I want to make sure that my Working is opened up so that that arrow is pointing down. I'm just going to drag one of these up to Working like that and when I release the mouse pointer, we get this little dialog box. I'm going to make sure that Copy items is checked. Add to targets is checked. And Finish. And this will copy the file, you see it's still down here, but it copies it and when I select that, there we have the file itself.
I'm going to clean up my workspace a little bit. I'm going to close this right pane. I'm going to open up the bottom pane and this is where the output from our command line programs will come in. This is debug space over there. We don't need that. So I'm going to close that left side and we have All Output selected and so now when I build and run this and under the Product menu, Build, which is Command B and Run, which is Command R. And so I'm just going to press Command B to build.
Build succeeded. And Command R to run. And we notice that it runs and it says Hello, World! And Program ended with exit code zero. So we'll get in to what all of this means and how the code works and everything later. Really the purpose of a Hello World program is simply to test out your development environment and your development cycle and make sure that you know how to run things in your environment, how to compile things in your environment, and how to work with the files.
That's really the entire purpose of Hello World. Now, at this point, you might want to adjust your font and your sizes and your line numbers. I always like to have line numbers. I find that very helpful and you know, adjust your workspace so that it works for you. This font size and color scheme is what we found works best for these videos, but obviously you're going to want to have more code on the screen. A lot of people prefer a darker screen.
It's less harsh on the eyes. The way that this is set up is really for the purpose of these videos and so that it works well for these videos. You may adjust your screen to your taste. Now, you'll notice also here under Product, there is Clean Build Folder and that's Shift Command K and if you open up the Products folder here, you'll see there's the executable Working program and when I clean the space, Shift Command K, you'll notice that that turns red, which means that that file is no longer there.
So that's been cleaned from the workspace. And then I can delete this file here by pressing Command Delete and Command Delete again to move it to the trash. And now our workspace is clean and ready for another exercise. And in this case, we're just going to drag over this cpp11.cpp and drop it in there and I can just press the Return key because all of my options here, you see all of my options remain the same, so I'm just going to press the Return key and that is now there in our Working folder and if I build and run and Command R will do both so Command R to build and run, and you notice that our output down here is exactly what we expect.
So this is just testing that this is a modern C++ compiler that has some of the modern features like this range based four loop, like this initialization scheme up here for the ray. These are things that work in modern compilers, but don't work in older compilers. So we've confirmed that we have a working installation and we're ready to follow the exercises. So I'm going to go ahead and delete my working copy here. Command Delete and Command Delete.
And I'm going to run Clean, Shift Command K, to clean up our workspace to be ready for the next lesson. So you now have a working Xcode workspace for following along with the exercise in this course and you more or less know how to manage that workspace. Xcode is an excellent IDE and Clang is a superb compiler and this combination will provide a great environment for this course.
- Setting up Xcode and Visual Studio
- Statements and expressions
- Primitive arrays and strings
- Data types
- Classes and objects
- Standard Library and Standard Template Library