How to Compile C Programs with GNU (GCC)

TipsMake today will teach you how to compile a C program from source code using GNU (full name is GNU Compiler Collection, abbreviated GCC) - compiler for Linux and Minimalist Gnu (MinGW) on Windows.

Using GCC on Unix

How to Compile C Programs with GNU (GCC) Picture 1How to Compile C Programs with GNU (GCC) Picture 1

Open the Unix Terminal window. This icon is usually dark blue with white characters.

How to Compile C Programs with GNU (GCC) Picture 2How to Compile C Programs with GNU (GCC) Picture 2

Type gcc --version and press ↵ Enter. This command will return the version number of the C compiler. If the command is not found, the GNU compiler may not be installed.

In this case, go through the documentation for your Linux distribution to learn how to get the package right.

If you are compiling a C++ program, use 'g++' instead of 'gcc.'

How to Compile C Programs with GNU (GCC) Picture 3How to Compile C Programs with GNU (GCC) Picture 3

Navigate to the folder where the source code is saved.

For example, if the source code file 'main.c' is located at /usr/wikiHow/source, type cd /usr/wikiHow/source.

How to Compile C Programs with GNU (GCC) Picture 4How to Compile C Programs with GNU (GCC) Picture 4

Enter the command gcc main.c –o HelloWorld. Replace 'main.c' with the name of the source file and 'HelloWorld' with the name of the completed program. Then the program will be compiled.

If you see an error message and want to see more information about the problem, use the command gcc -Wall -o errorlog file1.c. Then continue viewing the 'errorlog' file in the current directory with the command cat errorlog.

To compile a program from multiple source files, use the command gcc -o outputfile file1.c file2.c file3.c.

To compile multiple programs with multiple source files at the same time, use the command gcc -c file1.c file2.c file3.c.

How to Compile C Programs with GNU (GCC) Picture 5How to Compile C Programs with GNU (GCC) Picture 5

Launch the program you just compiled. Enter the command � 46;/HelloWorld, remembering to replace 'HelloWorld' with the name of your program.

By MinGW on Windows

How to Compile C Programs with GNU (GCC) Picture 6How to Compile C Programs with GNU (GCC) Picture 6

Download Minimalist GNU for Windows (MinGW). This is a very easy to install version of GCC for Windows. You can download the installer at https://sourceforge.net/projects/mingw/.

How to Compile C Programs with GNU (GCC) Picture 7How to Compile C Programs with GNU (GCC) Picture 7

Launch the MinGW installation program.

If the file doesn't open on its own, double-click the file in the download folder and then click Install.

How to Compile C Programs with GNU (GCC) Picture 8How to Compile C Programs with GNU (GCC) Picture 8

Customize your installation and then click Continue .

MinGW recommends users to use the default installation directory (C: MinGW). If you have to change it, you should also not use a folder with a name with spaces (eg 'Program Files').

How to Compile C Programs with GNU (GCC) Picture 9How to Compile C Programs with GNU (GCC) Picture 9

Select a compiler to install.

With minimal need, you can select Basic Setup in the left pane, then check next to all the compilers listed in the main right pane.

More advanced users can choose All Packages and additional compilers.

How to Compile C Programs with GNU (GCC) Picture 10How to Compile C Programs with GNU (GCC) Picture 10

Click the Installation menu in the upper left corner of MinGW.

How to Compile C Programs with GNU (GCC) Picture 11How to Compile C Programs with GNU (GCC) Picture 11

Click Apply Changes .

How to Compile C Programs with GNU (GCC) Picture 12How to Compile C Programs with GNU (GCC) Picture 12

Click Apply . The compiler will start downloading and installing.

How to Compile C Programs with GNU (GCC) Picture 13How to Compile C Programs with GNU (GCC) Picture 13

Add the path to MinGW to the system environment variables by:

Press ⊞ Win+S to launch the search bar, then type environment.

Click Edit the system environment variables in the search results that appear.

Click Environment Variables

Click Edit below the top pane (below the 'User Variables' line)

Scroll to the bottom of the 'Variable Value' pane.

Enter ;C:MinGWbin after the last word in the box. Note: if you have installed MinGW in another directory then enter the command ;C:path-to-that-directorybin, remember to replace 'path-to-that-directory' with the path to the actual directory.

Click OK twice. Click the remaining OK button to close the window.

How to Compile C Programs with GNU (GCC) Picture 14How to Compile C Programs with GNU (GCC) Picture 14

Open command prompt as administrator. To proceed, you:

Press ⊞ Win+S and type cmd.

Right-click Command Prompt in the search results and select Run As Administrator.

Click Yes to allow the changes.

How to Compile C Programs with GNU (GCC) Picture 15How to Compile C Programs with GNU (GCC) Picture 15

Navigate to the directory where your source code is saved.

For example, if the source file is called helloworld.c and is located at C:SourcePrograms, type cd C:SourcePrograms.

How to Compile C Programs with GNU (GCC) Picture 16How to Compile C Programs with GNU (GCC) Picture 16

Enter the command gcc helloworld.c –o helloworld.exe. You need to replace 'helloworld' with the source and application filenames. After the program is compiled, you will return to the command prompt without errors.

Any programming errors that appear should be fixed before compiling the program.

How to Compile C Programs with GNU (GCC) Picture 17How to Compile C Programs with GNU (GCC) Picture 17

Enter the name of the program to launch. If the program is called helloworld.exe then you need to enter this command to start the program.

4 ★ | 1 Vote