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
Step 1. Open a Terminal window on your Unix computer
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".
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
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.
Step 5. Run the compiled program with the command./programname
Method 2 of 2: Using MinGW for Windows
Step 1. Download Minimalist GNU for Windows from
MinGW is an easy-to-install GCC package for Windows.
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
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)
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.
Step 5. Click the Installation menu in the top left corner of MinGW
Step 6. Click Apply Changes
Step 7. Click Apply to download and install the compiler
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.
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.
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
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
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.