You are on page 1of 2

Using the g++ compiler on a Unix-based system

1. Create your program using a text editor on your computer system. Emacs, vi, pico are some of the more common text editors on unix systems. The program should be saved as a file with an extension of .cpp, .cc, or .C. Remember that any unix operating system is case-sensitive. So the name you choose for your program file must be used exactly as named. For example, a simple program that prints hello to the output screen might be saved as hello.cpp on your computer system. 2. Now you are ready to compile your program, hello.cpp, into an executable file. At the operating system command line prompt you give the command: g++ hello.cpp (Be sure to press enter after typing the command.) This command instructs the g++ compiler to compile your source file, hello.cpp, into an executable program. (This really involves 2 steps in one command, compile, then link into an executable program.) On unix-based systems, the executable program is automatically named a.out. (The programmer has the option to change this to a more descriptive name which is explained in the next section.) If any errors occur in the program the g++ compiler will report these errors to the output screen and will not create the executable program. You must correct any errors with your text editor and compile the program again. You repeat steps 1 and 2 until no errors are reported and your executable program is successfully created. 3. To execute the program itself, the command is: ./a.out This should execute your program on your unix system.

OPTIONAL g++ compiler commands on a Unix-Based system

1. To compile a program and rename the executable file with a name other than the default name, a.out, the command is:

g++ -o hello.out hello.cpp

This creates an executable file named hello.out from hello.cpp To execute the file you give the command: ./hello.out 2. To compile a program ONLY but not link and not create an executable file the command is:

g++ -c hello.cpp

This will generate an object file: hello.o which is not executable, but rather is a machine code version of your program (an object file), that can later be linked and made into an executable file.

Working with Multiple C++ Source Code Files For One Program
1. To create an executable program that has more than one C++ source code file the command is:

g++ file1.cpp file2.cpp file3.cpp This will generate one executable file, a.out, from the source code files that
make up the entire program, assuming no compiler errors occur during the compilation of all the files listed on the command line.

2. To link several previously compiled object files into one executable program file the command is: This will generate an executable file named a.out, which can then be executed. 3. To change the executable file name with multiple source code files use the following command:

g++ file1.o file2.o file3.o

g++ -o lab7.out file1.cpp file2.cpp file3.cpp This will generate an executable file named lab7.out, which can then be
executed. A simple explanation of a makefile Here are some very simple examples of various ways to create a makefile: make file example #1 make file example #2 make file example #3 For another explanation of makefiles see this : makefile tutorial. For a more complete explanation see this <="" a="" style="color: rgb(85, 119, 104);">

<="" a="" style="color: rgb(85, 119, 104); font-family: verdana, geneva, helvetica, sans-serif; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-size-adjust: auto; webkit-text-stroke-width: 0px;">
Last revised: 08/23/2004 18:11:26

You might also like