Using G++ For C++ Programming
Using G++ For C++ Programming
To start with, you run g++ simply by typing g++ at a command prompt (for Mac OS X that normally involves
starting Terminal.app and on Linux you can start up a similar terminal program, Gnome and KDE both
provide one). When you run g++ on its own with no arguments you should get something similar to this as a
response:
To actually compile a C++ file, you need to call g++ with the filename containing the C++ code. So if your
code is contained in a file called main.cpp, to compile it, just type 'g++ main.cpp'. Assuming you have no
errors, this will produce an executable file called '\1 (on Linux and Mac OS X). To run your resulting
executable, just type './a.out'. This is about the most basic thing you can do to produce an executable from
your C++ code. However, g++ is actually a very powerful optimizing compiler and provides you with many
possible options. To get a glimpse of what g++ provides you can visit http://gcc.gnu.org/ and browse the
documentation section or you type man g++ or info g++ from your command prompt to see the documentation
that shipped with your version of g++.
Since the number of options provided by g++ is vast, only a few basic options will be covered here and you
can look into other options as you see fit.
The first option to look at is the -o switch that g++ provides. In the example above, the compiler extracted the
code from your file and created a new file called 'a.out'. If you want to override this default name and instead
call the output executable file main, you would execute the following command 'g++ main.cpp -o main'.
When g++ finishes, it will have created an output file called 'main' instead of 'a.out' and to run it, you just
type './main'. You should try some different examples to get familiar with how this works.
The second option that is worth looking at is the -Wall switch. C++ is complex language and it can often be
unforgiving. So C++ compiler writers often look for ways to help programmers avoid writing bugs. The -
Wall switch that g++ provides is one such way. What the -Wall flag does is tell g++ to generate warnings for
any code that it encounters that is syntactically correct but looks suspicious semantically. In many cases -Wall
can uncover bugs before you even run the program because they are common mistakes and g++ can know to
look for them. Here are some useful examples.
1 #include <iostream>
2
3 using namespace std;
4
5 int
6 main (void)
7 { int a = 3;
https://www.cs.drexel.edu/~mcs171/Sp14/extras/g++/index.html 1/3
9/26/2017 Using g++ for C++ Programming
8 cout << "++a is: " ++a << " and a++ is: " << a++ << endl;
9
10 return 0;
11 }
At first glance, this looks ok but this actually contains the worst type of bug possible in C++, the dreaded
"undefined behavior"!
Assuming the code above is in a file called main.cpp, the command 'g++ main.cpp -o main' returns with no
complaints. However, compiling with 'g++ -Wall main.cpp -o main' gives the following output:
main.cpp: In function 'int main()':
main.cpp:8: warning: operation on 'a' may be undefined
On finding something suspicious, g++ gives you the file and the line number of the offending code. Pretty
useful. Admittedly, this is a relatively new warning heuristic in g++ and if you have an earlier version of g++
it may remain silent. However, it shows you that the compiler can really help you track down bugs if you take
advantage of its abilities. There are many more examples of options that your compiler provides that you
should try to take advantage of.
However, it should be noted that sometimes g++ gives false positives when using -Wall but in general, you
should be wary of code that generates warnings.
IO Redirection:
C++ provides the cin and cout for doing simple i/o operations and that is fairly useful. However, sometimes
you need a bit more flexibility and generating input and output files dynamically in C++ is not always
practical. IO redirection is one such way to gain a bit more flexibility without having to do any fancy C++
coding.
Most old and modern operating system provide two default "files", stdin and stdout. stdin, when used from a
terminal, is a way of sending keyboard input to a program. stdout, when used from a terminal, is a way of
sending output data to the terminal screen. However, it can be useful to redirect that input from a different
source or the output to a different destination. This is where IO redirection comes in handy. There is actually
quite a bit to IO redirection but this will only cover the basics of redirecting stdin and stdout. The examples
that will be used here, will revolve around using a C++ executable 'a.out' file. To recap, a.out is the default
output executable file produced by g++ when it compiles C++ code from some source file(s).
1 #include <iostream>
2
3 using namespace std;
4 int
5 main (void)
6 { int a;
7 cout << "Give me a number: ";
8 cin >> a;
9 cout << "You gave me: " << a << "!" << endl;
10 return 0;
11 }
When you run the command './a.out', the program prints out
Give me a number:
and waits for you to enter a number and hit return/enter. But what if you had a file, called number.txt that
contained just a single number. You can actually use that file as a source for your 'a.out' program when it
https://www.cs.drexel.edu/~mcs171/Sp14/extras/g++/index.html 2/3
9/26/2017 Using g++ for C++ Programming
runs by typing './a.out < number.txt'. Now instead of waiting for you to type a number in, it immediately
prints out the number that was in the file number.txt and the program ends. This example is a bit contrived but
it shows that you are not limited to a keyboard for the source of stdin.
1 #include <iostream>
2
3 using namespace std;
4 int
5 main (void)
6 { int a = 3;
7 cout << "The value of a is: " << a << "!" << endl;
8 return 0;
9 }
If the above code is compiled, you can use the following command to redirect the stdout to a file by typing
the command './a.out > output.txt'. As a word of warning, when you use > to redirect stdout, if the file
output.txt already exists prior to you running the previous command, the command will result in the file being
overwritten with whatever the program 'a.out' outputs via cout. If output.txt did not exist, the file will be
created for you automatically. After running the command, you can look at the contents of output.txt and see
that it is the same as if you just ran './a.out' with no redirection.
Earlier, it was noted that running './a.out > output.txt' will overwrite the output.txt if it already exists.
Sometimes we would like to prevent that and have whatever 'a.out' outputs via cout to be appended to the
end of the file output.txt instead. This can be accomplished by using >> instead of >. So, if for instance, you
run the command './a.out < output.txt' and then immediately follow it with './a.out << output.txt'. If
you look inside the file output.txt, the cout output from 'a.out' should be duplicated.
Both stdin and stdout redirection can be used at the same time. If we use the following code,
1 #include <iostream>
2
3 using namespace std;
4 int
5 main (void)
6 { int a;
7 cin >> a;
8 cout << "The value of a is: " << a << "!" << endl;
9 return 0;
10 }
and run the command, './a.out < number.txt > output.txt' then 'a.out' will read in from number.txt and
it send output to output.txt.
The ability to redirect input and output is a powerful mechanism and many of the programs in the Unix
environment make heavy use of this ability to effectively transfer input and output to different programs.
https://www.cs.drexel.edu/~mcs171/Sp14/extras/g++/index.html 3/3