Login: Ubuntu
Your Unix Account
Our C++ program (source code) will be entered as a file. This is the actual file that you will type using the editor. Other files will be used along with your source code to enable the execution of your program. For example, another type of file that may make up your project is a "header" file, or "include" file. An "include" file allows you to use additional source code that you or others have written. They comprise a library of helpful tools -- for example, they can provide input/output capabilities, standard math functions (like cosine, or finding the square-root), among others. In fact, they are often called standard library files. You reference a "header" file by "including" it into your source file. You will include a library file today that provides basic input and output services for your program (details to follow).
The complete collection of files needed to run our program is termed a project. So, a project is the collection of files that make up the program (or application) that you are developing.
Step-by-step instructions to creating the "HELLO WORLD" program
|
STEP
|
INFORMATION
|
|
| 0 | Directory setup:
|
|
| 1 | Open your preferred text editor (Applications => Accessories => "gedit Text Editor" or "GNU Emacs 23" or "GVim Text Editor" (the last one is known as "vi")). gedit is the simplest to start with. | |
|
||
| 2 |
The editor window is where you type in
your source code.
The editor uses something called syntax coloring to make it easier for you to read the programs you have written. Syntax coloring highlights the different program elements such as comments, keywords, numbers, and variables. This allows you to easily identify elements of your source code and find common syntax mistakes quickly. For example, gedit highlights comments in blue. If you see your source code is blue then you likely forgot to close a comment block. Additionally, the editor attempts to help in other ways such as by providing automatic indenting, aligning braces, and so forth.
|
|
| 3 | Create your main and add your include statements. If you do not understand what you are typing DO NOT BE CONCERNED. Your instructor will soon explain required and essential elements of C++ programs. Type the code exactly as shown below. | |
|
||
When done, save your file as follows:
| ||
| 4 |
Compile. If you want the
programs you write to actually do something then you
must compile them (turn source code into machine
language). Converting high-level C++ source code into
machine language is very complex, but compiler vendors
solve this problem for you. To compile your program,
first make sure that you have a terminal open from before, whose
current directory is where you saved main.cpp.
Once you are there, compile your
program by typing into the terminal
.
If your program compiled successfully, you should not see
any output from
If you do see text output from |
|
| 5 |
Link. Other files will be used along with your
source code to enable the computer to execute the program. For
example, another type of file that may make up your program
is a "header" file, or "include" file. An "include" file
allows you to use additional source code that you or others
have written. They comprise a library of helpful tools -- for
example, they can provide input/output capabilities, standard
math functions (like cosine, or finding the square-root),
among others. In fact, they are often called standard library
files. You reference a "header" file by "including" it into
your source file. You will include a library file today that
provides basic input and output services for your program
(details to follow). These additional files must be linked to
yours in a process called linking. To do this, type
in the
terminal ("-o" is an argument that is used to specify the name of the output file, which is "m012345" in this case. "main.o" is the object file that you created above.).
|
|
| 6 |
Run! Once you successfully compiled
and linked your program (no errors), then you are ready to
execute your program. In your current working
directory, you will see a new file called
m012345 (or whatever you have named it). That is
your program. To execute a file, type the file's name preceded
by a "dot slash" (literally, "./") and hit the enter key.
|
|
| 7 | Program Output: Your program will output to the same terminal from where it was executed, as seen above. |
Now, let's view the executable file you just executed.
Type ls -l to list details of the contents of
your current working directory. There is a file named
main.cpp – it is your source code file. Note the size
of the file (95 byes in our example). The file called m012345 is your program's
executable file, which is combined with all of the other
necessary machine code to run your program. Note the size
(7782 bytes in our example)!! The increase in size is due to the code that was
“included” or linked into your program. You should also see main.o listed, with
a size in between the other two.
Repeat Part 2 of this lab all over again, with these changes:
add.cpp instead of main.cpp. Now, what do you need to change when you compile your program?
// (Your name and alpha)
// This program adds two numbers
#include<iostream>
int main()
{
int number1, number2, sum;
number1 = 12;
number2 = 13;
sum = number1 + number2;
std::cout << "The sum of these two integers is " << sum << std::endl;
return 0;
}
|
Show your instructor when you finish this part.
Experience and practice help a lot here. So for each of the below, purposefully make the syntax error in your current program and try to compile to see what the error message is. Correct the error before introducing the next error into your program:
Show your instructor when you finish this part.
You'll spend the rest of your time learning more about Linux and the more powerful editors emacs/vi. Complete as many of the following steps as possible:
ls" frequently to see what effect the commands are having.You should now be comfortable with being able to generate source code, compile it, execute the program, and correct basic syntax errors -- all in Linux. Let's clean up so you can get to your next class.
|
|