Compiling a C Program with the GNU Compiler (with Pictures)

Table of contents:

Compiling a C Program with the GNU Compiler (with Pictures)
Compiling a C Program with the GNU Compiler (with Pictures)
Anonim

This wikiHow teaches you how to compile a C program from source using the GNU Compiler (GCC) for Linux and Minimalist Gnu (MinGW) for Windows.

Steps

Method 1 of 2: Using GCC for Unix

Compile a C Program Using the GNU Compiler (GCC) Step 1

Step 1. Open a terminal window on your Unix system

Compile a C Program Using the GNU Compiler (GCC) Step 2

Step 2. Type gcc --version and press ↵ Enter

This should return the version number of the C compiler. If the command is unknown, chances are that GCC is not installed.

  • If not installed, see the documentation for your Linux distribution to learn how to get the correct package.
  • If you want to compile a C++ program, use 'g++' instead of 'gcc'.
Compile a C Program Using the GNU Compiler (GCC) Step 3

Step 3. Navigate to the directory where you saved the source code

For example, if the source file, 'main.c' is located in /usr/wikiHow/source, you would type cd /usr/wikiHow/source

Compile a C Program Using the GNU Compiler (GCC) Step 4

Step 4. Type gcc main.c –o HelloWorld

Replace 'main.c' with the name of the source file, and 'HelloWorld' with the name of your program. The program is then compiled.

  • If you get errors and want more information about them, use gcc -Wall -o errorlog file1.c. Then view the file 'errorlog' in the current directory, with cat errorlog.
  • To compile one program from multiple source files, use gcc -o outputfile file1.c file2.c file3.c.
  • To compile multiple programs simultaneously from multiple source files, use gcc -c file1.c file2.c file3.c.
Compile a C Program Using the GNU Compiler (GCC) Step 5

Step 5. Run your compiled program

Type./HelloWorld but replace 'HelloWorld' with the name of your program.

Method 2 of 2: Using MinGW for Windows

Compile a C Program Using the GNU Compiler (GCC) Step 6

Step 1. Download Minimalist GNU for Windows (MinGW)

This is an easy to install version of GCC for Windows. You can download the installation file from

Compile a C Program Using the GNU Compiler (GCC) Step 7

Step 2. Run the MinGW installation file

If the file doesn't open by itself, double-click it in your downloads folder, then click 'Install'

Compile a C Program Using the GNU Compiler (GCC) Step 8

Step 3. Select your installation preferences and click Continue

MinGW recommends using the default installation folder (C:\MinGW). If you need to change the folder, don't use a folder with spaces in its name (such as "Program Files")

Compile a C Program Using the GNU Compiler (GCC) Step 9

Step 4. Select the compilers to install

  • In any case, choose Basic Setup in the left pane, and check the desired compilers in the main pane on the right.
  • Advanced users can choose All Packages and select additional compilers.
Compile a C Program Using the GNU Compiler (GCC) Step 10

Step 5. Click the Installation menu

This is located at the top left of the MinGW window.

Compile a C Program Using the GNU Compiler (GCC) Step 11

Step 6. Click Apply Changes

Compile a C Program Using the GNU Compiler (GCC) Step 12

Step 7. Click Apply

The compilers are now downloaded and installed.

Compile a C Program Using the GNU Compiler (GCC) Step 13

Step 8. Add the path to MinGW to your system's environment variables

Do this as follows:

  • Press ⊞ Win+S to select Search, and type environment.
  • click on Editing system environment variables in the search results.
  • click on Environment variables
  • click on To process under the top box (under 'User Variables')
  • Scroll to the bottom of the Variable Value box.
  • Type;C:\MinGW\bin immediately after the last letter in the box. Note: if you installed MinGW in a different directory, type;C:\path-to-that-directory\bin.
  • click on OK and then again OK. Click on the remaining OKbutton to close the window.
Compile a C Program Using the GNU Compiler (GCC) Step 14

Step 9. Open Command Prompt as administrator

You do this as follows:

  • Press ⊞ Win+S and type cmd.
  • Right click on Command Prompt in the search results, and after Run as administrator.
  • click on Yes to confirm the changes.
Compile a C Program Using the GNU Compiler (GCC) Step 15

Step 10. Navigate to the folder where you saved the source code

For example, if your source code is called helloworld.c and is located in C:\Source\Programs, then type cd C:\Source\Programs

Compile a C Program Using the GNU Compiler (GCC) Step 16

Step 11. Type gcc helloworld.c –o helloworld.exe

Replace 'helloworld' with the name of your source code and application. Once the program has been compiled, you will return to the command prompt without any errors.

Any errors in the code that are indicated must first be corrected before the program will compile

Compile a C Program Using the GNU Compiler (GCC) Step 17

Step 12. Type the name of your program to run it

If it's called helloworld.exe, you type that into the command line to start the program.

Tips

  • Compiling your source code with a -g flag will generate debug information that the corresponding debug program (GDB) can use to make debugging easier.
  • A 'makefile' can be created to make it easier to compile large programs.
  • If you use optimization a lot, keep in mind that optimization for speed can come at the expense of size and sometimes accuracy, and vice versa.
  • When compiling a C++ program, use G++ in the same way as GCC. Remember that C++ files have the extension '.cpp', instead of '.c'.

Popular by topic