User Tools

Site Tools


c_compile

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.

  • Change the working directory to the location in which the .c or .cpp file you wish to compile is contained.
  • Type one of the following commands (where program is the name of your file):
  • C Files
  • gcc program.c
  • Example: gcc assignment1.c
  • C++ Files
  • g++ program.cpp
  • Example: g++ assignment2.cpp
  • There should now be an executable file named a.out in the same directory you compiled the C or C++ file.
  • 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
c_compile.txt · Last modified: 2021/11/30 21:02 by john.nordlie