How to Compile a C Program Using the GNU Compiler (GCC)

This wikiHow teaches you how to compile a C program from source code by using the GNU Compiler (GCC) for Linux and Minimalist Gnu (MinGW) for Windows. Open up a terminal window on your Unix system. Its icon usually is a black screen with...
Method 1 of 2:

Using GCC for Unix

  1. How to Compile a C Program Using the GNU Compiler (GCC) Picture 1How to Compile a C Program Using the GNU Compiler (GCC) Picture 1
    Open up a terminal window on your Unix system. Its icon usually is a black screen with some white characters on it.
  2. How to Compile a C Program Using the GNU Compiler (GCC) Picture 2How to Compile a C Program Using the GNU Compiler (GCC) Picture 2
    Type gcc --version and press Enter. This should return the version number of the C compiler. If the command is not found, it is likely that GCC isn't installed.[1]
    1. If it's not installed, consult the documentation for your Linux distribution to learn how to get the correct package.
    2. If you're compiling a C++ program, use 'g++' instead of 'gcc.'
  3. How to Compile a C Program Using the GNU Compiler (GCC) Picture 3How to Compile a C Program Using the GNU Compiler (GCC) Picture 3
    Navigate to the directory where your source code is saved.
    1. For example, if your source code file, 'main.c', is located in /usr/wikiHow/source, type cd /usr/wikiHow/source.
  4. How to Compile a C Program Using the GNU Compiler (GCC) Picture 4How to Compile a C Program Using the GNU Compiler (GCC) Picture 4
    Type gcc main.c –o HelloWorld. Replace 'main.c' with the name of your source code file, and 'HelloWorld' with the name of your finished program. The program will now compile.
    1. If you see errors and want to see more information about them, use gcc -Wall -o errorlog file1.c. Then, view the 'errorlog' file in the current directory with cat errorlog.
    2. To compile one program from multiple source code files, use gcc -o outputfile file1.c file2.c file3.c.
    3. To compile multiple programs at once with multiple source code files, use gcc -c file1.c file2.c file3.c.
  5. How to Compile a C Program Using the GNU Compiler (GCC) Picture 5How to Compile a C Program Using the GNU Compiler (GCC) Picture 5
    Run your newly-compiled program. Type ./HelloWorld but replace 'HelloWorld' with the name of your program.
Method 2 of 2:

Using MinGW for Windows

  1. How to Compile a C Program Using the GNU Compiler (GCC) Picture 6How to Compile a C Program Using the GNU Compiler (GCC) Picture 6
    Download Minimalist GNU for Windows (MinGW). This is an easy-to-install version of GCC for Windows. You can download the installer from https://sourceforge.net/projects/mingw/.[2]
  2. How to Compile a C Program Using the GNU Compiler (GCC) Picture 7How to Compile a C Program Using the GNU Compiler (GCC) Picture 7
    Run the MinGW installer.
    1. If the file doesn't open by itself, double-click it in your downloads folder, then click Install.
  3. How to Compile a C Program Using the GNU Compiler (GCC) Picture 8How to Compile a C Program Using the GNU Compiler (GCC) Picture 8
    Select your install preferences and click Continue.
    1. MinGW recommends using the default installation folder (C:MinGW). If you must change the folder, don't use a folder with spaces in the name (e.g. 'Program Files').[3]
  4. How to Compile a C Program Using the GNU Compiler (GCC) Picture 9How to Compile a C Program Using the GNU Compiler (GCC) Picture 9
    Select which compilers to install.
    1. At the minimum, choose Basic Setup on the left panel, then place check marks next to all of the listed compilers in the right main panel.
    2. More advanced users can choose All Packages and select additional compilers.
  5. How to Compile a C Program Using the GNU Compiler (GCC) Picture 10How to Compile a C Program Using the GNU Compiler (GCC) Picture 10
    Click the Installation menu. It's at the top left corner of MinGW.
  6. How to Compile a C Program Using the GNU Compiler (GCC) Picture 11How to Compile a C Program Using the GNU Compiler (GCC) Picture 11
    Click Apply Changes.
  7. How to Compile a C Program Using the GNU Compiler (GCC) Picture 12How to Compile a C Program Using the GNU Compiler (GCC) Picture 12
    Click Apply. The compilers will now download and install.
  8. How to Compile a C Program Using the GNU Compiler (GCC) Picture 13How to Compile a C Program Using the GNU Compiler (GCC) Picture 13
    Add the path to MinGW to system environment variables. Here's how:
    1. Press Win+S to launch search, then type environment.
    2. Click Edit the system environment variables in the search results.
    3. Click Environment Variables
    4. Click Edit beneath the top box (under 'User Variables')
    5. Scroll to the end of the 'Variable Value' box.
    6. Type ;C:MinGWbin right after the last letter in the box. Note that if you installed MinGW to a different directory, enter ;C:path-to-that-directorybin.
    7. Click OK, and then OK again. Click the one remaining OK button to close the window.
  9. How to Compile a C Program Using the GNU Compiler (GCC) Picture 14How to Compile a C Program Using the GNU Compiler (GCC) Picture 14
    Open the command prompt as an administrator. To do this:
    1. Press Win+S and type cmd.
    2. Right-click Command Prompt in the search results, then select Run As Administrator.
    3. Click Yes to allow changes.
  10. How to Compile a C Program Using the GNU Compiler (GCC) Picture 15How to Compile a C Program Using the GNU Compiler (GCC) Picture 15
    Navigate to the folder where your source code is saved.
    1. For example, if your source code file called helloworld.c is located in C:SourcePrograms, type cd C:SourcePrograms.
  11. How to Compile a C Program Using the GNU Compiler (GCC) Picture 16How to Compile a C Program Using the GNU Compiler (GCC) Picture 16
    Type gcc helloworld.c –o helloworld.exe. Replace 'helloworld' with the name of your source code and application. Once the program is compiled, you'll return to the command prompt without errors.[4]
    1. Any coding errors that appear must be corrected before the program will compile.
  12. How to Compile a C Program Using the GNU Compiler (GCC) Picture 17How to Compile a C Program Using the GNU Compiler (GCC) Picture 17
    Type the name of your program to run it. If it's called helloworld.exe, type that to start your program.
3.5 ★ | 2 Vote