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

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

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

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

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.'

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

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.

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

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.

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

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

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

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/.

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

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.

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

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').

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

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.

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

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

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

Click Apply Changes .

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

Click Apply . The compiler will start downloading and installing.

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

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.

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

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.

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

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.

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

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.

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

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.

Update 25 February 2022
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile