A compiler is software that reads a source file and generates a new file containing all the instructions in machine language which can be understood by a computer. In this video, learn how to compile and execute a C++ program.
- [Narrator] Before we start coding in C++, it's important that you understand how to compile and run C++ programs. A compiler is software that reads a source file and generates a new file containing all the instructions in machine language which can be understood by the computer. Do you know what machine language looks like? If you said it's a series of ones and zeros, you're absolutely correct. So, the machine language is just a series of what we call bits. Each bit can either be on or off and is represented with a one or a zero.
Now, it would be really difficult for us to program in machine language cause we'd have to figure out how to supply all the ones and zeros and that would just take a really, really long time. So, we use a compiler to accomplish that task. To actually execute a C++ program, it first must be compiled. Here's a visual representation of what happens. We start with our source file. In this case it's called program.cpp where the extension .cpp stands for C++.
That file is fed into the compiler software. The software takes our source code and generates a machine language file which we will refer to as the object file. Usually it has an extension of .o in C++. That file is then fed into the linker. The linker then generates an executable file, in this case program.exe. In this example, we only had one source file. You could have multiple source files.
They're all compiled by the compiler to create their object files and then multiple files can be fed into the linker together to create one executable file. And we'll see some of that when we start programming. When programming, many developers use what we call an IDE. IDE stands for integrated development environment which combines all the tools needed for development into one tool called an IDE. The IDE contains a source code editor, which usually provides syntax highlighting, color coding, automatic indentation, brace matching, and more.
It also has a C++ compiler and a runtime environment. So, it brings all those tools together into one which makes programming a little easier. There are several free, open source IDEs. Here's a few that I found. Code::Blocks which is available at http://www.codeblocks.org. That IDE will run on Windows, Linux, and the Mac OS. Another popular IDE is Visual Studios Express found at http://www.microsoft.com/express/.
And that runs on Windows. And finally, Dev-C++. This actually can be found at http://orwelldevcpp.blogspot.com. And that also runs on Windows. If you prefer to use a Linux or a Mac environment, your machine probably has the development features needed to compile your programs directly from a terminal just by including the appropriate C++ flags in the command line for the compiler.
Here I have a list of those flags. So, if you're using a GCC compiler, again this is for Mac OS and Linux, you're going to want to use the command g++ -std=c++0x example.ccp as the name of your program. So, this particular one is called example, but you're program will probably have a different name. Space-o and then another space and then the executable it's going to create is whatever word comes next and in this case we have example_program.
The clang compiler is also for OS X and the command is very similar except for it starts with clang for the c language and then the ++. Once you create your executable code, if you're running from a Mac OS or a Linux terminal window, to run the program you're going to type ./example_program which is the name of the executable file. When using the command prompt, first you want to navigate to the folder that contains your .cpp source code file.
This is required to compile and run programs using a terminal window on your Mac. From the terminal window, navigate to that folder before using the commands. I've included a list of some of the commands that I've found useful when using Linux and Mac. Here's a table. So, the command ls is similar to the command dir on the PC which gives you a list of the directory contents. In order to change directory, they also use cd, but cd space .. is how you would change to the parent directory.
It's a little different cause in the Windows environment, you would just do cd.. but on the Mac, you have to do cd space .. In Windows, to clear the screen, you would type cls, here we type the word clear, which clears the window. We have mk to allow us to make a directory. And rmdir to remove a directory. Pwd which when you first look at it makes me think of password, but actually that's a command you can use to see what is the current working directory that I'm in.
So, it prints the working directory. And echo, echo can be used to display text in a window. This is a subset of some of the commands that I found more useful while working on a Linux machine or a Mac OS. If you want to learn more, check out the course by Scott Simpson here in our library called Learn the Linux Command Line: The Basics. For this course, I will be using Dev-C++ which is a free open source download and because I'm using a Windows machine. Once we have Dev-C++ downloaded, we'll need to add information for the compiler to include the new features for C++ 11.
Let me show you how. First, you'll go to tools, compiler options in the general tab, then you're going to type -std=c++11. I'm going to switch over to the Dev-C++ IDE. So, you can see, I've already done this. So, if I go to tools, and in the very top we have compiler options, notice under the general tab, you want to make sure that you check off the box that says add the following commands when calling the compiler and you want to add -std=c++11.
That will ensure that all the new commands we're using with this new release are included when my programs are compiled. Okay, I hope you have a good understanding now of how the process of writing source code, compiling the program, running it through the linker, and then creating the executable works for C++. Again, I'm going to be using a Windows environment, but what's really nice about C++ is that it really runs on any OS, whether it's a Linux machine, a Mac OS, or a Windows machine.
- Compiling and running C++ programs
- Creating variables in C++
- Choosing the correct data type to represent variables
- Creating assignment expressions
- Changing data types in C++
- Comparing values in C++
- Using logical operators
- Comparing strings in C++
- Defining and calling a function
- Using arrays in C++