Title: Hello World using g
1Hello World using g
- Writing programs with XEmacsInstead of using an
integrated environment for programming, as you
did with Visual Studio, we'll use separate tools
for compiling, debugging, editing source code,
etc. under Unix. This is pretty typical for Unix.
// g -c hello.cpp // g -o hello
hello.o include ltiostreamgt using namespace
std int main() cout ltlt "Hello world" ltlt
endl
- Programming starts with a text editor, which is,
after all, what you use to type in your program,
so we'll start there too using the xemacs editor.
2Compiling with the open source compiler g
- To compile, we use the -c option. Compiling a
.cpp file (i.e. source file) produces a .o file
(i.e. object) file. - g -c hello.cpp // produces hello.o
- The link stage links together your object files
and any libraries you might be using to create an
executable file, which is an actual "program". - For example, to take the object file hello.o we
just compiled and create a program named hello,
we'd type - g -o hello hello.o // produces hello (an
executable) - Note that if you don't provide a name for the
executable, the default name is a.out.
3g with multifile programs
// main.cpp include ltiostreamgt include
"fact.h using namespace std int main() cout
ltlt fact(5) ltlt endl return 0
// fact.h ifndef fact_h define fact_h int
fact(int n) endif
// fact.cpp include "fact.h" int fact(int n)
if (n 0) return 1 return nfact(n-1)
- To create an executable factorial from these 3
source code files, we need to
g -c fact.cpp //creates fact.o g -c
main.cpp //creates main.o g -o factorial
main.o fact.o //creates factorial
4Incremental compilation
- To change main so that 6! is output instead of
5! - We would modify main.cpp appropriately, and
recompile. But what really needs to be
recompiled? - Clearly fact.cpp hasn't changed a bit, so there
is no reason to recompiled it. Thus, having
edited main.cpp, we can produce our new
executable with - g -c main.cpp
- g -o factorial main.o fact.o
- This idea that as changes are made to a program
we only recompile the things that have changed,
rather than recompiling every source file every
time, is called incremental compilation.
5Compile time, link time, run time
- People typically refer to three stages in which
things "happen" concerning a program compile
time, link time and run time (or execution time).
Understanding at which stage something happens
can really help you understand it. This is
especially true of errors. - A compile time error is something like a missing
semicolon. These are things that can be
determined from the information available to you
in a given compilation unit. - A link time error involves some sort of mismatch
across two or more compilation units. Example
providing two different definitions matching the
same prototype - or maybe for providing no
definitions for a given prototype. - A run time error only crops up once the program
is running - like writing beyond the end of an
array, or forgetting a base case in a recursive
function definition.
6Shortcuts for compilation
- To make our lives (a bit) easier, g can do the
separate compilations and linking steps for us in
a single line if we simply give it the -o name
option and list all the source files involved. - For the factorial example, this would be
- g -o factorial main.cpp fact.cpp
- Obviously, this is more convenient. In fact, g
simply automatically breaks things up into the
three steps we explicitly listed above. So the
same things happen when we use this convenient
short-hand.
7In Class Exercise (ICE) Saving Pennies
- Write a program that computes the amount of money
you would have in a year if you saved 1 penny the
first day, 2 more pennies the second day (for a
total of 3 cents), 3 more pennies the third day
(for a total of 6 cents), and so forth. - Compile and run your program using g