Basic C++ Compiling Under Linux
Compiling and writing programs under the Linux operating system is more complex and cumbersome than in the Visual Studio .NET development environment. Writing a program and compiling a program are handled by different applications. While it is trivial to write a program using Xemacs, it is more difficult to compile the written program.
The g++ Compiler
Programs can be compiled using a compiler called g++. While many advanced command-line options are available (which you can look up using the "--help" option), the basic syntax is "g++ -o executable_name source_file(s)", where executable_name is the name that the compiled program sould have, and source_files(s) are the ".cpp" files that compose the program (or the main file from which others are included). More detailed use of the compiler will provided later, but for now, the following basic usage example should suffice:
g++ -o myprog myfile.cpp
Executing the Program
Once the program has been successfully compiled, it can be run by prefixing "./" to the executable name, as in:
./myprog
While it may not always be necessary to specify that the program to be executed is in the current directory (by using the "./" prefix), it is often the case. If the prefix is not used, the system may return an error message stating that the specified program was not found or is invalid.
Capturing Program Output
Since it is often necessary to show test cases for programs that students develop, and since that output may involve more lines than the terminal buffer will hold, it is often necessary to use a small text-capturing program. One of the easiest programs to use is script. The only argument to the program is the filename for the output. While the output filename is optional, it is better to specify a file than to use the default file, as data may be overwritten if you've already stored data in it. To start the capture program, use the following command to store output to the file "output.txt":
script output.txt
The capture program will record all keystrokes and program output that occur either until the terminal window is closed, or the command exit is issued. Issuing the exit command will close the script program and save the output session to the specified file.
Removing ^M Characters
It is often the case that files brought from a Windows system to a Linux system syffers a conversion error - there are "^M" characters at the end of every line. This problem arises from the fact that Windows system treat every press of the Enter key as a carriage return and a line feed. On Linux/UNIX systems, the Enter key is interpreted only as a line feed, while the remaining carriage return becomes the irksome "^M" character. To remove it, issue the following commands that open the alternate (and cryptic) vi editor and modify the affected file:
vi filename
:%s/ ctrl-v enter //g
:wq
These commands should remove all the problematic characters from your sight.
On This Page...
The g++ Compiler
Executing the Program
Capturing Program Output
Removing ^M Characters