How to Compile C Programs with GNU (GCC)

Using GCC on Unix

How 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 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 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 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 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 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 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 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 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 10

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

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

Click Apply Changes .

How 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 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 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 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 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 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

May be interested

  • How to Uninstall a ProgramHow to Uninstall a Program
    uninstalling old programs is an important maintenance task for most modern devices. old programs take up space and can slow you down. broken programs can cause problems with your system. removing programs is usually fairly simple. if...
  • How to Use Add or Remove Programs in Windows XPHow to Use Add or Remove Programs in Windows XP
    windows xp has a facility of add or remove programs to modify or delete installed programs, add new programs or to simply manage defaults of programs. let's see how to use this facility. continue to step one to start. click on start and...
  • C ++ editor onlineC ++ editor online
    online cplusplus editor has a very fast execution time, detects errors related to the syntax as soon as you type the code with a red x right on the error code, color the code ... and run as soon as you code by clicking go to execute.
  • How to Launch Programs from Command Line on LinuxHow to Launch Programs from Command Line on Linux
    most linux distributions have a graphical user interface that allows us to open programs just by clicking on the program icon in the apps menu. however, sometimes you may want to launch programs from terminal. terminal is a powerful tool that allows users to launch programs and manage linux systems through keyboard commands. today's tipsmake will show you how to launch programs from terminal on linux.
  • Remove 'root' programs that haven't been uninstalled completely on Windows 7, XPRemove 'root' programs that haven't been uninstalled completely on Windows 7, XP
    sometimes in some cases after you have removed a program or application that programs or applications are still present in the list of currently installed programs on add or remove program or programs and features in control panel.
  • How to remove all available applications on Windows 10?How to remove all available applications on Windows 10?
    just like the samsung phones, there are quite a lot of programs on windows 10 that we ... never use. but they do not appear in control panel, so how to remove all those programs? in the article below, tipsmake.com will share with you some steps to solve that problem.
  • Spyware replaces virus in enterprise computersSpyware replaces virus in enterprise computers
    spyware is becoming more dangerous and sophisticated but users still cannot take the most basic measures to protect themselves. recent statistics show that nearly 30% of us employees have become victims of programs and programs
  • HTML editor onlineHTML editor online
    online html editor supports html5, when you type the opening tag automatically generates a closed tag, can detect errors related to the syntax when you are editing with the square icon with the x mark right on the error code line.
  • How to Remove Programs from Windows with IObit UninstallerHow to Remove Programs from Windows with IObit Uninstaller
    over the course of the life of your windows pc, you've probably installed a bunch of programs. some of them you use, and some of them just take up space. every so often, you may want to do some spring cleaning and remove these unwanted...
  • 8 ways to open Programs and Features tool on Windows8 ways to open Programs and Features tool on Windows
    the programs and features tool shows you a complete list of applications installed on your pc.