CMPT
130 - Getting Started
In
this activity you are going to create a simple program that prints a message to
the console. The activity should be
completed before the first lab on the 13th of September. You should use Linux (in either room 4050 or
4080 – the two Surrey CS labs) which will mean making sure that the machine is
booted in Linux.
Finding
Applications in Linux
In
Linux you can find applications in a number of different ways. The easiest way is to click on Applications in the top left corner of
the window. This gives you a list of applications in a similar interface to the
Windows Start menu.
Part 1 -
Create Program in a text editor
Open
a text editor application. I would suggest using either Sublime Text (in the Programming folder) or gedit (in the
Accessories folder). Both provide C++ text
highlighting; Sublime Text also has some other nice features. Type the
following, don't copy and paste it.
/* Hello World */
#include
<iostream>
int main()
{
std::cout << std::endl << "Hello World!" << std::endl
return 0;
}
If
you've programmed in C, C++ or Java before you may notice a missing semi-colon
after the cout
statement. This is a deliberate error.
Now
save the file, and call it hello.cpp,
but don't close it after saving it (as we haven't finished with it yet). Note that I've asked you to call the file hello.cpp
- the .cpp is the file's extension and identifies it as a C++
source file (in the same way that calling a program something.docx
identifies it as a Word file).
Part 2 -
Compiling Your Program in the Terminal
Now
we will compile and run the program in the Terminal. First open
the Terminal, which you can find in
Utilities. Then to compile the program enter the
following at the prompt in the Terminal.
g++ –o hello
hello.cpp
At
this point you will see an error message, since we "forgot" the
semi-colon after the cout
statementl.
You will have to go back to the file to fix it.
The
–o is a "flag" which instructs the compiler to name the object file
the given name (hello in this case).
If I'd wanted to call the object file bob
for some reason I would have compiled it like this.
g++ –o bob
hello.cpp
If
you don't use –o and name the object file it will be called a.out by default. This will then
get overwritten the next time you compile a source file so I find it useful to
always give the output file a sensible name.
Finding Your File
One thing that could go wrong here is that you may
have saved your file somewhere other than the default location. If so, g++
won't be able to find the file to compile it. You can fix this in two ways.
1.
Give g++ the full name of the
file including its file path.
2.
Change the folder that the terminal is
pointing to. This can be done by typing cd and the name of the folder
you want to move to.
Part 3 -
Debugging Your Program
Go
back to the hello.cpp file in your
text editor and fix the error by typing a semi-colon so that the first line
under int main() looks
like this:
std::cout << std::endl << "Hello
World!" << std::endl;
Then
compile the program again by running the command g++ –o hello hello.cpp in the terminal, it should work this time,
without giving you any errors. It won't
give you a happy success message either, so if it does work you shouldn't see
anything except the next command prompt.
Part 4 -
Running Your Program
Finally,
run your program by entering its name in the terminal, you have to precede this
with the characters ./ like this:
./hello
You
will then see the Hello World message
printed in the terminal.
John Edgar (johnwill@sfu.ca)