This article was co-authored by wikiHow staff writer, Travis Boylls. Travis Boylls is a Technology Writer and Editor for wikiHow. Travis has experience writing technology-related articles, providing software customer service, and in graphic design. He specializes in Windows, macOS, Android, iOS, and Linux platforms. He studied graphic design at Pikes Peak Community College.
The wikiHow Tech Team also followed the article's instructions and verified that they work.
This article has been viewed 944,534 times.
Learn more...
This wikiHow teaches you how to compile a C program from source code by using the GNU Compiler (GCC) for Linux and Minimalist Gnu (MinGW) for Windows.
Steps
GCC for Linux
-
1Open up a terminal window on your Linux system. Its icon usually is a black screen with some white characters on it. You can usually find it in your Applications menu.
-
2Install GCC. If you do not have GCC already installed, you can use the following Terminal commands to install GCC for Ubuntu and Debian.[1] For all other versions of Linux, consult the documentation for your Linux distribution to learn how to get the correct package:
- Type sudo apt update and press "Enter" to update the package list.
- Type sudo apt install build-essential and press "Enter" to install the essential packages, which include GCC, G++, and Make.
- Type sudo apt-get install manpages-dev and press "Enter" to install the manual pages.
Advertisement -
3Type gcc --version and press ↵ Enter. This will verify that GCC is installed properly and return the version number. If the command is not found, it is likely that GCC isn't installed.
- If you’re compiling a C++ program, use “g++” instead of “gcc.”
-
4Navigate to the directory where your source code is saved. Use the cd command to navigate directories in the Terminal. For example, if your source code is in your Documents folder you would type cd /home/[username]/Documents (in Ubuntu). You can also navigate to the Documents directory by typing cd ~/Documents in the Terminal.
-
5Type gcc [program_name].c –o [executable_name] and press ↵ Enter. Replace “[program_name].c” with the name of your source code file, and “[executable_name]” 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.
-
6Run your newly-compiled program. Type ./[executable_name] but replace “[executable_name]” with the name of your program.
MinGW for Windows
-
1Download Minimalist GNU for Windows (MinGW). This is an easy-to-install version of GCC for Windows. Use the following steps to download MinGW:.[2]
- Go to https://sourceforge.net/projects/mingw/ in a web browser.
- Click the green button that says Download.
- Wait for the installer to download automatically.
-
2Install MinGW. Use the following steps to install MinGW:
- Double-click mingw-get-setup.exe in your Downloads folder or web browser.
- Click Install.
- 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”).
Advertisement -
3Select 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.
-
4Right-click each package and click Mark for Installation. The Basic Setup has about 7 packages listed in the box at the top. Right-click each one of them (or just the ones you want) and click Mark for Installation. This adds an icon with an arrow next to each one and marks it for installation.
-
5Install the selected packages. It may take your computer several minutes to install all packages. Use the following steps to install the packages that are marked for installation.
- Click the Installation menu in the upper-left corner.
- Click Apply Changes.
- Click Apply.
- Click Close once the installation is done.
-
6Add the path to MinGW to system environment variables. Use the following steps to add the path to MinGW to system environtment variables:
- Type environment in the search bar next to the Start menu.
- Click Edit the system environment variables in the search results.
- Click Environment Variables
- Select the Path variable.
- Click Edit beneath the top box (under “User Variables”)
- Click New.
- Type C:\MinGW\bin in the new space. Note that if you installed MinGW to a different directory, enter C:\path-to-that-directory\bin.
- Click OK, and then OK again. Click the one remaining OK button to close the window.
-
7Open the command prompt as an administrator. You must be signed in to a Windows account with administrative privileges to open the Command Prompt as an administrator. Use the following steps to open the Command Prompt as an administrator:
- Type cmd in the search bar next to the Start menu..
- Right-click Command Prompt in the search results, then select Run As Administrator.
- Click Yes to allow changes.
-
8Navigate to the folder where your source code is saved. For example, if your source code file called helloworld.c is located in C:\Source\Programs, type cd C:\Source\Programs
-
9Type gcc c –o [program_name].exe [program_name].c and press ↵ Enter. Replace “[program_name]” with the name of your source code and application. Once the program is compiled, you’ll return to the command prompt without errors.[3]
- Any coding errors that appear must be corrected before the program will compile.
-
10Type the name of your program to run it. If it’s called hello_world.exe, type that in the command prompt to start your program.
- If you receive an "Access is denied" or "Permission denied" error message when compiling a program or running the output executable file, check the folder permissions and make sure you have full read/write access to the folder that contains the source code. If that doesn't work, try temporarily disabling your virus software.[4]
Community Q&A
-
QuestionWhat do I do if there are two tables in the environmental variables window? In which table and directory should I add an environmental variable?Community AnswerUse the upper table entitled "User Variables for" and select the "Path" directory.
-
QuestionWhat is a good compiler for C language?Community AnswerThe MinGW GCC compiler will work well.
-
QuestionIs the extension .exe necessary in a compiling program?Community AnswerTheoretically, as long as a file contains the necessary binary machine code, it could be run by a processor. One example is how on Windows, a screensaver is simply a program (just like a .exe) but with the .scr extension. However, as far as I know, most modern operating systems will not attempt to execute a file if it does not identify the file type as being executable, so you would need to somehow trick the operating system into actually starting a new process using the code from the file.
References
About This Article
1. Open the Terminal in Linux.
2. Type "sudo apt update" and press Enter.
3. Type "sudo apt install build-essential" and press Enter to install GCC and other packages.
4. Type "gcc --version" to verify the GCC installation.
5. Use the "CD" command to navigate to the folder with your source code.
6. Type "gcc [program_name].c –o [executable_name]" to compile the program.
7. Type ""./[executable_name]" to run the newly compiled program.