How to Compile C Programs with GNU (GCC)
Using GCC on Unix
Open the Unix Terminal window. This icon is usually dark blue with white characters.
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.
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.'
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.
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 completed program. 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, 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.
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
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/.
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.
Customize your installation and then click Continue .
MinGW recommends users to use the default installation directory (C: MinGW). If you have to change it, you should also not use a folder with a name with spaces (eg 'Program Files').
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.
Click the Installation menu in the upper left corner of MinGW.
Click Apply Changes .
Click Apply . The compiler will start downloading and installing.
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' pane.
Enter ;C:MinGWbin after the last word in the box. Note: if you have installed MinGW in another directory then 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.
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.
Navigate to the directory 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.
Enter the command gcc helloworld.c –o helloworld.exe. You need to replace 'helloworld' with the source and application filenames. After the program is compiled, you will return to the command prompt without errors.
Any programming errors that appear should be fixed before compiling the program.
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.
You should read it
- 15 ways to keep smiles on your lips even when life is toughest
- Change color between different lines in Microsoft Excel
- Google integrates WebRTC on Chrome
- To touch twice to unlock the screen on your Samsung phone
- How the laptop is called ultrabook
- Create your own Linux distribution with Ubuntu Imager
- 10 tips to change the registry in Windows XP and Vista
- Why are there identical twins, some are not?
May be interested
- How to Design and Implement a Field Using C Sharpthe first step in designing a class usually is to define its fields. fields are the backbone that classes rely upon, and so, the process of designing and implementing fields is crucial to the overall process of designing classes. this...
- How to Make Your Windows Forms Application Run at Windows Startup Using Csometimes you need to run your windows forms application when the user log in to windows. many applications that use this behavior are windows live messenger and yahoo messenger. in this article you will learn how to make your application...
- How to Set Up an OpenGL SDL GLEW Template Project in Visual Studiomany programmers prefer opengl for graphics. if you are one of them, you are strongly advised by its producer, to use a window toolkit (such as sdl) and an opengl loading libraries (such as glew). this guide will help you get over the...
- How to Use Function Template Parameter Packs in C++c++ template parameter packs (also known as variadic templates) were introduced in the c++11 standard and are powerful tools that let functions accept an arbitrary number of arguments. without further ado, jump into your favourite ide,...
- How to Create a 20 Questions Game in C++this tutorial will walk you through creating 20 questions in c++ with numbers using visual studio. this tutorial is very 'bare bones' and only uses the basics of c++ programming. obtain a copy of visual studio and open it.
- How to Create a Recursive Function in C++recursion is a major topic in computer science. it is a method where a problem is reduced or simplified to smaller versions of itself until a solution is achieved. this tutorial will demonstrate how to write a simple recursive function in...