User Tools

Site Tools


How to Compile a C or C++ File

Compiling a C / C++ file requires the use of Windows Command Prompt (Windows), Terminal/Shell (Mac & Unix), or an IDE.

NOTE: The following steps are for compiling a C / C++ program in a Unix Terminal session.

  1. Change the working directory to the location in which the .c or .cpp file you wish to compile is contained.

  2. Type one of the following commands (where program is the name of your file):

    1. C Files

      • gcc program.c

      • Example: gcc assignment1.c

    2. C++ Files

      • g++ program.cpp

      • Example: g++ assignment2.cpp

  3. There should now be an executable file named a.out in the same directory you compiled the C or C++ file.

    1. For compiling C or C++ files with a specific name instead of a.out, add -o filename to the compile command.

      • gcc -o homework1 assignment1.c will create homework1.out

      • g++ -o homework2 assignment2.cpp will create homework2.out

how_to_compile_a_c_or_c_file.txt · Last modified: 2013/06/25 10:05 by jhuber