How to Compile a C Program Using the GNU Compiler (GCC)
Method 1 of 2:
Using GCC for Unix
- Open up a terminal window on your Unix system. Its icon usually is a black screen with some white characters on it.
- Type gcc --version and press ↵ Enter. This should return the version number of the C compiler. If the command is not found, it is likely that GCC isn't installed.[1]
- If it's not installed, consult the documentation for your Linux distribution to learn how to get the correct package.
- If you're compiling a C++ program, use 'g++' instead of 'gcc.'
- Navigate to the directory where your source code is saved.
- For example, if your source code file, 'main.c', is located in /usr/wikiHow/source, type cd /usr/wikiHow/source.
- Type gcc main.c –o HelloWorld. Replace 'main.c' with the name of your source code file, and 'HelloWorld' with the name of your finished program. The program will now compile.
- If you see errors and want to see more information about them, use gcc -Wall -o errorlog file1.c. Then, view the 'errorlog' file in the current directory with cat errorlog.
- To compile one program from multiple source code files, use gcc -o outputfile file1.c file2.c file3.c.
- To compile multiple programs at once with multiple source code files, use gcc -c file1.c file2.c file3.c.
- Run your newly-compiled program. Type ./HelloWorld but replace 'HelloWorld' with the name of your program.
Method 2 of 2:
Using MinGW for Windows
- Download Minimalist GNU for Windows (MinGW). This is an easy-to-install version of GCC for Windows. You can download the installer from https://sourceforge.net/projects/mingw/.[2]
- Run the MinGW installer.
- If the file doesn't open by itself, double-click it in your downloads folder, then click Install.
- Select your install preferences and click Continue.
- MinGW recommends using the default installation folder (C:MinGW). If you must change the folder, don't use a folder with spaces in the name (e.g. 'Program Files').[3]
- Select which compilers to install.
- At the minimum, choose Basic Setup on the left panel, then place check marks next to all of the listed compilers in the right main panel.
- More advanced users can choose All Packages and select additional compilers.
- Click the Installation menu. It's at the top left corner of MinGW.
- Click Apply Changes.
- Click Apply. The compilers will now download and install.
- Add the path to MinGW to system environment variables. Here's how:
- Press ⊞ Win+S to launch search, then type environment.
- Click Edit the system environment variables in the search results.
- Click Environment Variables
- Click Edit beneath the top box (under 'User Variables')
- Scroll to the end of the 'Variable Value' box.
- Type ;C:MinGWbin right after the last letter in the box. Note that if you installed MinGW to a different directory, enter ;C:path-to-that-directorybin.
- Click OK, and then OK again. Click the one remaining OK button to close the window.
- Open the command prompt as an administrator. To do this:
- Press ⊞ Win+S and type cmd.
- Right-click Command Prompt in the search results, then select Run As Administrator.
- Click Yes to allow changes.
- Navigate to the folder where your source code is saved.
- For example, if your source code file called helloworld.c is located in C:SourcePrograms, type cd C:SourcePrograms.
- Type gcc helloworld.c –o helloworld.exe. Replace 'helloworld' with the name of your source code and application. Once the program is compiled, you'll return to the command prompt without errors.[4]
- Any coding errors that appear must be corrected before the program will compile.
- Type the name of your program to run it. If it's called helloworld.exe, type that to start your program.
3.5 ★ | 2 Vote
You should read it
May be interested
- Huawei launched Ark Compiler to help Android outperform iOShuawei's ark compiler compiler will increase android performance more than ios.
- How to Compile CPP File to EXEthis wikihow teaches you how to convert your c++ code (cpp) into an executable exe file. if you use the commercial version of microsoft visual studio to write your code, it has a built-in compiler that's easy to use. if you're using a...
- How to Compile Python Scriptpython is a very popular language for programming. but what if the person running your program does not want or know how to run a python script? this article will teach you how to compile a python script into an executable. download...
- Installation instructions Cthere are several free and available ides for compiling and executing c programs. you can choose dev-c ++, code :: blocks, or turbo c. however, the most popular and commonly used options are especially dev-c ++ and c programs in this series are also compiled and executed in dev-c ++.
- Inline function in C ++inline functions in c ++ are powerful concepts that are commonly used with classes. if a function is inline, the compiler places a copy of the code of that function at each location that the function is called at compile time.
- CSS online editorthe online css compiler tool allows css coding in the browser and immediately sees the effects it brings to make your css learning easier and easier.
- Instructions on how to compile and execute Java using Command Promptlet's tipsmake.com refer to the tutorial how to compile and execute java by command prompt on windows and mac computers!
- Command line parameter in Cthis chapter only really makes sense for you if you are using a command promt to compile the program. it is possible to pass values from the command line - command line to program c when it is executed.
- Python online editorthis python online compiler has two parts: the above is called script.py, where you enter python code. click run code when writing the code, the output will display below, in the ipython shell section.
- TOP 6 websites that support online Python programming compilationhere is a list of the best online python compilers that you can use to compile, script, and run your code in python online.