• About
  • Contact
  • Cookie
  • Disclaimer
  • Privacy Policy
  • Change the purpose of use

Tnhelearning.edu.vn - Various useful general information portal

  • Photo
  • Bio
  • How To
  • Tech

How to Compile C Programs with GNU (GCC)

January 22, 2024 by admin Category: How To

You are viewing the article How to Compile C Programs with GNU (GCC)  at Tnhelearning.edu.vn you can quickly access the necessary information in the table of contents of the article below.

X

This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.

The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.

This article has been viewed 16,454 times.

This wikiHow teaches you how to compile a C program from source code using GNU (full name is GNU Compiler Cplection, abbreviated GCC) – a compiler for Linux and Minimalist Gnu (MinGW) on Linux. Windows.

Table of Contents

  • Steps
    • By GCC on Unix
    • By MinGW on Windows
  • Advice
  • Things you need

Steps

By GCC on Unix

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

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/7/7f/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-1-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-1-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/7/7f/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-1-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-1-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
Open a Unix Terminal window. This icon is usually dark blue with white characters.
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 2

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/c/cc/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-2-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-2-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/c/cc/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-2-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-2-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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. [1] X Research Source

  • 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.”
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 3

Image titled Compile a C Program Using the GNU Compiler (GCC) Step 3

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/e/e0/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-3-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-3-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/e/e0/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-3-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-3-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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 .
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 4

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/b/bb/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-4-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-4-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/b/bb/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-4-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-4-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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 program after completion. 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, you 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 .
READ More:   How to Uninstall NVIDIA Driver
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 5

Image titled Compile a C Program Using the GNU Compiler (GCC) Step 5

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/9/94/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-5-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-5-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/9/94/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-5-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-5-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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

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

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/b/b9/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-6-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-6-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/b/b9/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-6-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-6-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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/. [2] X Research Source
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 7

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/0/03/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-7-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-7-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/0/03/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-7-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-7-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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.
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 8

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/3/39/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-8-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-8-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/3/39/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-8-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-8-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
Customize your installation and then click Continue .

  • MinGW recommends users to use the default installation directory ( C:MinGW ). If you have to change, you should also not use a folder with a name with spaces (for example, “Program Files”). [3] X Research Sources
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 9

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/3/3c/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-9-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-9-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/3/3c/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-9-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-9-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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.
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 10

Image titled Compile a C Program Using the GNU Compiler (GCC) Step 10

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/2/23/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-10-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-10-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/2/23/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-10-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-10-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
Click the Installation menu in the upper left corner of MinGW.
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 11

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/b/b3/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-11-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-11-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/b/b3/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-11-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-11-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
Click Apply Changes .
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 12

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/b/bd/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-12-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-12-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/b/bd/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-12-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-12-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
Click Apply . The compiler will start downloading and installing.
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 13

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/b/b4/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-13-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-13-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/b/b4/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-13-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-13-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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” box.
  • Enter ;C:MinGWbin after the last word in the box. Note: if you have installed MinGW in another directory, 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.
READ More:   How to Treat Redness
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 14

Image titled Compile a C Program Using the GNU Compiler (GCC) Step 14

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/3/34/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-14-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-14-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/3/34/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-14-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-14-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
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.
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 15

Image titled Compile a C Program Using the GNU Compiler (GCC) Step 15

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/e/ee/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-15-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-15-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/e/ee/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-15-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-15-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
Navigate to the folder 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 .
Image titled Compile a C Program Using the GNU Compiler (GCC) Step 16

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

{“smallUrl”:”https://www.wikihow.com/images_en/thumb/a/ae/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-16-Version-2 .jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-16-Version-2.jpg”,”bigUrl”:”https://www.wikihow. com/images/thumb/a/ae/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-16-Version-2.jpg/v4-728px-Compile-aC-Program-Using -the-GNU-Compiler-%28GCC%29-Step-16-Version-2.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing” :”<div class=”mw-parser-output”></div>”}
Enter the command gcc helloworld.c –o helloworld.exe . You need to replace “helloworld” with the source and application file names. After the program is compiled, you will return to the command prompt with no errors. [4] X Research Sources

  • Any programming errors that appear should be fixed before compiling the program.
  • Image titled Compile a C Program Using the GNU Compiler (GCC) Step 17

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

    {“smallUrl”:”https://www.wikihow.com/images_en/thumb/7/7b/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-17.jpg/v4 -728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-17.jpg”,”bigUrl”:”https://www.wikihow.com/images/thumb/7/ 7b/Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29-Step-17.jpg/v4-728px-Compile-aC-Program-Using-the-GNU-Compiler-%28GCC%29- Step-17.jpg”,”smallWidth”:460,”smallHeight”:345,”bigWidth”:728,”bigHeight”:546,”licensing”:”<div class=”mw-parser-output”></ div>”}
    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.
  • Advice

    • Building code with the (flag) -g flag will generate debug information that the respective GDB debugger can use for better performance.
    • Makefiles can be created to make compiling large programs easier.
    • If you apply a lot of optimization, be cautious because optimizing for speed can be a trade-off with size, sometimes program accuracy, and vice versa.
    • When compiling a C++ program, you can use G++ in the same way as you would with GCC. Note: C++ files will have the extension .cpp instead of .c.

    Things you need

    • Computer running Linux or Windows operating system
    • Basic knowledge of GNU/Linux and how to install applications.
    • Program code
    • Text editor (e.g. Emacs, vi)
    READ More:   How to Deal with People Who Talk Behind Your Back
    X

    This article is co-authored by a team of editors and trained researchers who confirm the accuracy and completeness of the article.

    The wikiHow Content Management team carefully monitors the work of editors to ensure that every article is up to a high standard of quality.

    This article has been viewed 16,454 times.

    This wikiHow teaches you how to compile a C program from source code using GNU (full name is GNU Compiler Cplection, abbreviated GCC) – a compiler for Linux and Minimalist Gnu (MinGW) on Linux. Windows.

    Thank you for reading this post How to Compile C Programs with GNU (GCC) at Tnhelearning.edu.vn You can comment, see more related articles below and hope to help you with interesting information.

    Related Search:

    Related Posts

    How to Create Curved Text in Photoshop
    How to fall asleep faster
    How to Install FBReader to Read eBooks

    Category: How To

    Previous Post: « How to make hot and sour shark soup for family meals
    Next Post: Should makeup by abusing powder cream? »

    Copyright © 2025 · Tnhelearning.edu.vn - Useful Knowledge