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

Open up a terminal window on your Unix system. , Type gcc --version and press ↵ Enter., Navigate to the directory where your source code is saved., Type gcc main.c –o HelloWorld., Run your newly-compiled program.

5 Steps 1 min read Medium

Step-by-Step Guide

  1. Step 1: Open up a terminal window on your Unix system.

    This should return the version number of the C compiler.

    If the command is not found, it is likely that GCC isn't installed.If it’s not installed, consult the documentation for your Linux distribution to learn how to get the correct package.

    If you’re compiling a C++ program, use “g++” instead of “gcc.” , For example, if your source code file, “main.c”, is located in /usr/LifeGuide Hub/source, type cd /usr/LifeGuide Hub/source. , Replace “main.c” with the name of your source file, and “HelloWorld” with the name of your finished program.

    The program will now compile.

    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.

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

    To compile multiple programs at once with multiple source files, use gcc
    -c file1.c file2.c file3.c. , Type ./HelloWorld but replace “HelloWorld” with the name of your program.
  2. Step 2: Type gcc --version and press ↵ Enter.

  3. Step 3: Navigate to the directory where your source code is saved.

  4. Step 4: Type gcc main.c –o HelloWorld.

  5. Step 5: Run your newly-compiled program.

Detailed Guide

This should return the version number of the C compiler.

If the command is not found, it is likely that GCC isn't installed.If it’s not installed, consult the documentation for your Linux distribution to learn how to get the correct package.

If you’re compiling a C++ program, use “g++” instead of “gcc.” , For example, if your source code file, “main.c”, is located in /usr/LifeGuide Hub/source, type cd /usr/LifeGuide Hub/source. , Replace “main.c” with the name of your source file, and “HelloWorld” with the name of your finished program.

The program will now compile.

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.

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

To compile multiple programs at once with multiple source files, use gcc
-c file1.c file2.c file3.c. , Type ./HelloWorld but replace “HelloWorld” with the name of your program.

About the Author

D

Deborah Brooks

Enthusiastic about teaching practical skills techniques through clear, step-by-step guides.

96 articles
View all articles

Rate This Guide

--
Loading...
5
0
4
0
3
0
2
0
1
0

How helpful was this guide? Click to rate: