Compiling and Running a C++ Program on Hercules

Compile Command

Let's suppose you had a file called hello.cpp in your current working directory. The command to compile this program is:
CC -o hello hello.cpp

Now, looking at each part of that command:
CC This is the command that says "Compile my C++ program."
Notice that this is capital CC not lower case. Unix is very "case sensitive" i.e. there can be a huge difference in meaning between characters in upper case and those same characters in lower case.
-o hello This is called the "minus-oh" option.
It tells the linker to create an executable file as specified by the name following -o. It is a convention to name your executable file the same name as the source file without the .cpp extension. Without the minus-oh option your executable file will be, by default, a.out
hello.cpp This is the name of the C++ program.
Notice that you need to use the extension .cpp

Compiling and Linking Individual Program Modules

Use the -c option to compile a C++ module, but not produce an executable file. This will produce an object file with a .o extension. Call the C++ compiler again to link each .o file. e.g.
CC -c part1.cpp
CC -c part2.cpp
CC -o hello part1.o part2.o

Run Command

hello

That's all. Just type in the name of the executable file. Following the example just given, the name of the executable file is hello. If we had not specified the -o hello option, then the executable would have been named a.out and that is what we would have had to type to run the program.


This page has been accessed     times.
Last modified: Friday, 21-Aug-2020 15:28:14 CST
Copyright 2002 Department of Computer Science, University of Regina.


 CS Dept Home Page

Teaching Material