Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Laboratory Exercises, C++ Programming
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
The GNU Compiler Collection and <strong>C++</strong> 5<br />
named file.s. The -E option stops the driver after preprocessing and prints the result from this<br />
first stage on standard output.<br />
A1. Write a “Hello, world!” program in a file hello.cc, compile and test it.<br />
A2. Generate preprocessed output in hello.ii, assembly code output in hello.s, and object code<br />
in hello.o. Study the files:<br />
• hello.ii: this is just to give you an impression of the size of the header.<br />
You will find your program at the end of the file.<br />
• hello.s: your code starts at the label main.<br />
• hello.o: since this is a binary file there is not much to look at. The command nm<br />
hello.o (study the manpage) prints the file’s symbol table (entry points in the<br />
program and the names of the functions that are called by the program and must be<br />
found by the linker).<br />
2 Options and messages<br />
There are many options to the g++ command that we didn’t mention in the previous section. In<br />
the future, we will require that your source files compile correctly using the following command<br />
line:<br />
g++ -c -pipe -O2 -Wall -W -ansi -pedantic-errors -Wmissing-braces \<br />
-Wparentheses -Wold-style-cast file.cc<br />
Short explanations (you can read more about these and other options in the gcc and g++<br />
manpages):<br />
-c just produce object code, do not link<br />
-pipe use pipes instead of temporary files for communication between<br />
tools (faster)<br />
-O2 optimize the object code on “level 2”<br />
-Wall print most warnings<br />
-W print extra warnings<br />
-ansi follow standard <strong>C++</strong> syntax rules<br />
-pedantic-errors treat “serious” warnings as errors<br />
-Wmissing-braces warn for missing braces {} in some cases<br />
-Wparentheses warn for missing parentheses () in some cases<br />
-Wold-style-cast warn for old-style casts, e.g., (int) instead of static cast<br />
Do not disregard warning messages. Even though the compiler chooses to “only” issue warnings<br />
instead of errors, your program is erroneous or at least questionable. Another option, -Werror,<br />
turns all warnings into errors, thus forcing you to correct your program and remove all warnings<br />
before object code is produced. However, this makes it impossible to test a “half-written” program<br />
so we will normally not use it.<br />
Some of the warning messages are produced by the optimizer, and will therefore not be<br />
output if the -O2 flag is not used. But you must be aware that optimization takes time, and<br />
on a slow machine you may wish to remove this flag during development to save compilation<br />
time. Some platforms define higher optimization levels, -O3, -O4, etc. You should not use these<br />
levels, unless you know very well what their implications are. Some of the optimizations that are<br />
performed at these levels are very aggressive and may result in a faster but much larger program.