How to compile a C/C++ program


Question:
"I want to compile a C/C++ program in UNIX how do I do this?"
Answer:  you use the gcc command for a C program or g++ command for a C++ program
The gcc command compiles a C program, and g++ compiles a C++ program

You can compile a C program (written using a text editor such as vi) by using the gcc command. For example, if the name of your program file is "assignment1.c", to compile it you may type in the following:

johndoe on cyberserver.uml.edu>gcc assignment1.c

OR
To compile a C++ program called "assignment1.cpp":

johndoe on ce.uml.edu>g++ assignment1.cpp

At this point if there are errors in your program the compiler will show them on the screen and then return you to the prompt. You should then correct these errors by opening the program in your text editor.

After you have corrected your errors, you may use the same gcc or g++ command to compile it again. If there are no errors in your code the compiler will return you to the prompt without any error messages. This means that your code has been compiled into a separate executable file which by default is named as a.out. You may run the code by typing in a.out at the prompt as follows:

johndoe on ce.uml.edu>a.out

If you wish to give a specific name to your compiled program (suppose you want to call it "assignment1") you may do this using one of the following commands instead of the regular gcc or g++ command:

For a C program:
johndoe on ce.uml.edu>gcc -o assignment1 assignment1.c

OR
For a C++ program:
johndoe on ce.uml.edu>gcc -o assignment1 assignment1.c

You program will be compiled into a executable file called "assignment1". As in the case of "a.out" you may run this by simply typing in "assignment1" at the prompt as follows.

johndoe on ce.uml.edu>assignment1