Main

Compiling Code

Question: "I want to compile a C/C++ program in UNIX how do I do this?"

Answer:

You can use the gcc command for a C program or g++ command for a C++ program.

You can compile a C program (written by using a text editor such as vi or emacs) by using the gcc command. For example, if the name of your program file is test1.c, to compile it you may type in the following (after connecting to ix-dev with ssh ix-dev.cs.uoregon.edu):

 ix-dev> gcc test1.c

or, to indicate a specific C standard version, e.g., C 11:

 ix-dev> gcc -std=c11 test1.c

To compile a C++ program called test1.cpp with the 2014 version of the C++ standard:

 ix-dev> g++ -std=c++14 test1.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:

 ix-dev> ./a.out

Note that above the path to a.out is explicitly specified by prefixing it with ./, which assumes that you are running the a.out program in the same directory, in which you compiled it.

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

For a C program:

 ix-dev> gcc -o test1 test1.c

For a C++ program:

 ix-dev> gcc -o test1 test1.c

Your program will be compiled into an executable file called test1. As in the case of a.out you may run this by simply typing in ./test1 at the prompt as follows.

 ix-dev> ./test1

Green Marinee theme adapted by David Gilbert, powered by PmWiki