How to Compile C Programs with the GNU C Compiler (GCC)

Table of contents:

How to Compile C Programs with the GNU C Compiler (GCC)
How to Compile C Programs with the GNU C Compiler (GCC)

Video: How to Compile C Programs with the GNU C Compiler (GCC)

Video: How to Compile C Programs with the GNU C Compiler (GCC)
Video: How to boot macOS into safe boot or safe mode - macOS troubleshooting 2024, November
Anonim

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

Step

Method 1 of 2: Using GCC for Unix

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

Step 1. Open a Terminal window on your Unix computer

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

Step 2. Enter the command gcc --version and press Enter to display the GCC version on the computer

If your computer displays a command not found message, GCC may not be installed on your computer.

  • If needed, install GCC by following the guide for your Linux distribution.
  • To compile a C++ program, use "g++", instead of "gcc".
Compile a C Program Using the GNU Compiler (GCC) Step 3
Compile a C Program Using the GNU Compiler (GCC) Step 3

Step 3. Open the folder where you saved the source code of the program

For example, if the program code "akurapopo.c" is in the /usr/yuliaR/source folder, enter the command cd /usr/yuliaR/source

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

Step 4. Enter the command gcc akurapopo.c –o AkuRapopo

Replace "akurapopo.c" with your program's source code name, and "AkuRapopo" with your desired program name. The compilation process will start.

  • If an error occurs during the compilation process, collect the error information with the command gcc -Wall -o errorlog akurapopo.c. After that, display the "errorlog" file with the cat errorlog command.
  • Compile the program from several source code files with the command gcc -o programname file1.c file2.c file3.c.
  • To compile multiple programs from multiple source code at once, use the command gcc -c file1.c file2.c file3.c.
Compile a C Program Using the GNU Compiler (GCC) Step 5
Compile a C Program Using the GNU Compiler (GCC) Step 5

Step 5. Run the compiled program with the command./programname

Method 2 of 2: Using MinGW for Windows

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

Step 1. Download Minimalist GNU for Windows from

MinGW is an easy-to-install GCC package for Windows.

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

Step 2. Run the MinGW installation program

If the installation program does not open automatically, double-click the MinGW file in the Downloads folder, then click Install

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

Step 3. Adjust the program installation options, then click Continue

The recommended folder for installing MinGW is C:\MinGW. If you need to change the installation folder, do not select a folder with a name containing spaces (such as Program Files)

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

Step 4. Select the compiler you want to install

  • To install the recommended minimal compiler, select Basic Setup in the left pane, then tick all compilers that appear in the right main pane.
  • If needed, you can select All Packages and tick all additional compilers.
Compile a C Program Using the GNU Compiler (GCC) Step 10
Compile a C Program Using the GNU Compiler (GCC) Step 10

Step 5. Click the Installation menu in the top left corner of MinGW

Compile a C Program Using the GNU Compiler (GCC) Step 11
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
Compile a C Program Using the GNU Compiler (GCC) Step 12

Step 7. Click Apply to download and install the compiler

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

Step 8. Add the MinGW PATH to the system environment variable by following these steps:

  • Press Win+S to open the Search menu, then enter the environment keyword.
  • In the search results, click Edit the system environment variables.
  • Click Environment Variables.
  • Click Edit under the top box (below User Variables).
  • Scroll to the end of the Variable Values box.
  • Enter;C:\MinGW\bin at the end of the text in the box. If you installed MinGW in another folder, replace;C:\MinGW\bin with;C:\installationfoldername\bin.
  • Click OK twice to close the window.
Compile a C Program Using the GNU Compiler (GCC) Step 14
Compile a C Program Using the GNU Compiler (GCC) Step 14

Step 9. Open a command line window as Administrator by following these steps:

  • Press Win+S, then enter cmd.
  • Right-click Command Prompt in the search results, then click Run as Administrator.
  • Click Yes to allow changes to the computer.
Compile a C Program Using the GNU Compiler (GCC) Step 15
Compile a C Program Using the GNU Compiler (GCC) Step 15

Step 10. Open the folder where you saved the source code of the program

For example, if you saved the source code "lailacanggung.c" in the C:\Source\Programs folder, enter the command cd C:\Source\Programs

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

Step 11. Enter the command gcc lailacanggung.c –o lailacanggung.exe

Replace the filename with the name of your program code file. After the compilation process is complete, you will see the command line again. Errors that occur will not be displayed.

Errors in the program code must be corrected before starting the compilation process. The program code containing the error could not compile

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

Step 12. Enter the name of your program to run it, for example lailacanggung.exe

Tips

  • When you compile a program with the -g parameter, the compiler will include the appropriate debug information for GDB, GCC's built-in debug program. This information will make it easier for you to debug.
  • To compile large programs, you can first create a Makefile.
  • If your program is optimized for speed, the program size may increase, and the accuracy may not be very good. On the other hand, if you optimize the size or accuracy of the program, the speed of the program may decrease.
  • When compiling C++ programs, use G++ as you would GCC. C++ files have a.cpp extension instead of.c.

Recommended: