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
- The development flow of programming languages, new problems that are not new
- The interesting fact about the universe is not quite the same as what we thought
- 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?
- How to Identify if Your Phone Is an Original or a Clone
- My biggest 3 Google Home pet peeves and how to fix them
Maybe you are interested
What to do when open command window here does not appear?
How to switch users on the Linux command line
How to fix Mac Homebrew error 'zsh: command not found: brew'
What is the Command key on Windows?
How to use read command in Linux
Basic Linux commands everyone needs to know - Operations on Linux are much faster