How To Compile A C Program Using The GNU Compiler (GCC)

Skip to ContentQuizzes
  • Home
  • Random
  • Browse Articles
  • Learn Something New
  • QuizzesHot
  • Forums
  • Courses
  • Happiness Hub
  • Play Games
  • This Or That Game
  • Train Your Brain
  • Explore More
  • Support wikiHow
  • About wikiHow
  • Log in / Sign up
Terms of Use wikiHow is where trusted research and expert knowledge come together. Learn why people trust wikiHow How to Use GCC to Compile a C Program on Linux and Windows PDF download Download Article The complete guide to compiling C code with GCC on Linux and Windows Written by Nicole Levine, MFA

Last Updated: March 13, 2024 Fact Checked

PDF download Download Article
  • Using Linux
  • |
  • Using Windows Subsystem for Linux
  • |
  • Using MinGW for Windows
  • |
  • Video
  • |
  • Q&A
  • |
  • Tips
|Show more |Show less X

This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. There are 10 references cited in this article, which can be found at the bottom of the page. This article has been fact-checked, ensuring the accuracy of any cited facts and confirming the authority of its sources. This article has been viewed 1,176,159 times. Learn more...

Are you ready to turn your C code into an executable program? The GNU C compiler, also known as GCC, is a simple Linux-based C compiler that's easy to use from the command line. If you're using Linux, including Ubuntu, Fedora, and Linux Mint, you can install GCC from your distribution's package manager. On Windows 10 and 11, you can use GCC in a Windows Subsystem for Linux (WSL) shell, or by installing an open source tool called MinGW. This wikiHow guide will teach you the easiest ways to compile a C program from source code using GCC.

Things You Should Know

  • To make sure GCC is installed, run the command gcc --version.
  • Type gcc source_file.c -o program_name and press Enter to compile your source code.
  • Replace source_file with the name of your source code file, and program_name with the name you'd like to give your compiled program.

Steps

Method 1 Method 1 of 3:

Using Linux

PDF download Download Article
  1. Step 1 Install your distribution's build tools. 1 Install your distribution's build tools. Most versions of Linux don't come with GCC already installed. Fortunately, it's easy to install GCC and other required tools (including make, G++, and general development libraries) for compiling software on any version of Linux:
    • Ubuntu, Debian, & Linux Mint:
      • In a terminal window, type sudo apt update and press Enter.
      • Type sudo apt install build-essential and press Enter.[1]
      • Type gcc --version and press Enter to verify your installation.
    • CentOS, Fedora, and Red Hat:
      • In a terminal window, type sudo dnf group install "Development Tools" and press Enter.[2]
      • Type gcc --version and press Enter to verify your installation.
  2. Step 2 Go to the directory that contains your source code. 2 Go to the directory that contains your source code. Use the cd command to change to the directory in which you've saved the source code you want to compile.
    • For example, if your source code is in a folder called Documents that's inside your home directory, use cd ~/Documents.
    Advertisement
  3. Step 3 Type gcc -o [executable_name] [source_file].c  and press ↵ Enter. 3 Type gcc -o [executable_name] [source_file].c and press Enter. Replace “[source_file].c” with the name of your source code file, and “[executable_name]” with the name you'd like to give your compiled program.
    • If you need to compile a program from multiple source files, use the syntax gcc -o [executable_name] sourcefile1.c sourcefile2.c sourcefile3.c.
    • If you see errors and want to see more information about them, use gcc -Wall -o errorlog file1.c.[3] Then, view the “errorlog” file in the current directory with cat errorlog.
    • To compile multiple programs at once with multiple source code files, use gcc -c file1.c file2.c file3.c.
  4. Step 4 Run your newly-compiled program. 4 Run your newly-compiled program. Type ./[executable_name] but replace “[executable_name]” with the name of your program.
  5. Advertisement
Method 2 Method 2 of 3:

Using Windows Subsystem for Linux

PDF download Download Article
  1. Step 1 Open a bash shell in Windows Subsystem for Linux. 1 Open a bash shell in Windows Subsystem for Linux. If you're using Windows 10 or Windows 11 with Windows Subsystem for Linux installed, you can easily compile software with the GCC compiler. However, you'll need to install GCC first, as it isn't installed automatically.
    • If you haven't already enabled Windows Subsystem for Linux (WSL), you can install it with a single command from the command prompt or PowerShell. Start Command Prompt or PowerShell as an administrator, and then run this command to install Ubuntu: wsl --install.[4]
      • If you see the WSL help text when running the command, you may have already enabled WSL. Instead, run the command wsl --list –online to see a list of Linux distributions, then use wsl --install -d <DistroName> (e.g., Ubuntu) to install Ubuntu.
    • Once installed, you can open an Ubuntu bash shell by typing ubuntu into the Windows Search bar and clicking Ubuntu in the search results.[5]
  2. Step 2 Install the build tools for Ubuntu. 2 Install the build tools for Ubuntu. These tools include the GCC compiler, as well as all of the libraries you'll need to compile and debug C and C++ code.
    • Type sudo apt update and press Enter.
    • Type sudo apt-get install build-essential gdb and press Enter.[6]
  3. Step 3 Go to the directory that contains your source code. 3 Go to the directory that contains your source code. Use the cd commanad to change to the directory in which you've saved the source code you want to compile.
  4. Step 4 Type gcc -o [executable_name] [source_file].c  and press ↵ Enter. 4 Type gcc -o [executable_name] [source_file].c and press Enter. Replace “[source_file].c” with the name of your source code file, and “[executable_name]” with the name you'd like to give your compiled program.
    • If you need to compile a program from multiple source files, use the syntax gcc -o [executable_name] sourcefile1.c sourcefile2.c sourcefile3.c.
    • If you see errors and want to see more information about them, use gcc -Wall -o errorlog file1.c.[7] Then, view the “errorlog” file in the current directory with cat errorlog.
    • To compile multiple programs at once with multiple source code files, use gcc -c file1.c file2.c file3.c.
  5. Step 5 Run your newly-compiled program. 5 Run your newly-compiled program. Type ./[executable_name] but replace “[executable_name]” with the name of your program.
  6. Advertisement
Method 3 Method 3 of 3:

Using MinGW for Windows

PDF download Download Article
  1. Step 1 Download Minimalist GNU for Windows (MinGW). 1 Download Minimalist GNU for Windows (MinGW). This is an easy-to-install version of GCC for Windows. You can download the installer from https://osdn.net/projects/mingw.[8]
    • To get the latest version of the installer, click the mingw-get-setup.exe link.
    • If the download doesn't begin automatically, click Save or Download when prompted.
    • MinGW can only compile 32-bit versions of software. However, all 32-bit software compiled with MinGW will execute properly on a 64-bit system.[9]
    • If you need to compile 64-bit software, try MinGW-w64, a port of MinGW. You can download it from https://www.mingw-w64.org.[10] Alternatively, you can use Windows Subsystem for Linux.
  2. Step 2 Install MinGW. 2 Install MinGW. Once you've downloaded the installer, double-click it to install MinGW.
      • 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. Step 3 Select Basic Setup to view the basic C compiling tools. 3 Select Basic Setup to view the basic C compiling tools. If you want more options, you can select All Packages to see all available libraries and build tools.
  4. Step 4 Right-click each package and click Mark for Installation. 4 Right-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.
    • At the very least, you will want to install MinGW32-base and MinGW32-gcc-g++. If you need to compile code written in objective C, you should also mark MinGW32-gcc-objc for installation.
  5. Step 5 Install the selected packages. 5 Install 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.
  6. Step 6 Add the path to MinGW to system environment variables. 6 Add the path to MinGW to system environment variables. Use the following steps to add the path to MinGW to system environment 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 in the "System variables" section.
    • 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.
  7. Step 7 Open the command prompt as an administrator. 7 Open 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.
  8. Step 8 Go to the folder where your source code is saved. 8 Go 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 and press Enter.
  9. Step 9 Type gcc c –o [program_name].exe [program_name].c and press ↵ Enter. 9 Type 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.
    • Any coding errors that appear must be corrected before the program will compile.
  10. Step 10 Type the name of your program to run it. 10 Type the name of your program to run it. If it’s called hello_world.exe, type that in the command prompt and press Enter 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.[11]
  11. Advertisement

Community Q&A

Search Add New Question
  • Question What 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 Answer Community Answer Use the upper table entitled "User Variables for" and select the "Path" directory. Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 2 Helpful 8
  • Question What is a good compiler for C language? Community Answer Community Answer The MinGW GCC compiler will work well. Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 10 Helpful 8
  • Question Is the extension .exe necessary in a compiling program? Community Answer Community Answer Theoretically, 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. Thanks! We're glad this was helpful. Thank you for your feedback. If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow Yes No Not Helpful 2 Helpful 8
See more answers Ask a Question 200 characters left Include your email address to get a message when this question is answered. Submit Advertisement

Video

Tips

  • Building your code with a -g flag will produce debugging information which the corresponding debugger program, GDB, can use to make debugging work better. Thanks Helpful 0 Not Helpful 0
  • Makefiles can be created to make it easier to compile large programs. Thanks Helpful 0 Not Helpful 0
  • If you use optimizations heavily, be aware that optimization for speed may come with a trade off in size and sometimes accuracy, and vice versa. Thanks Helpful 0 Not Helpful 0
Show More Tips Submit a Tip All tip submissions are carefully reviewed before being published Name Please provide your name and last initial Submit Thanks for submitting a tip for review! Advertisement

You Might Also Like

Move from Windows to LinuxHow toMove from Windows to Linux Learn to Program in CA Beginner's Guide to Programming in C Edit DLL Files in Visual StudioHow toEdit DLL Files in Visual Studio Delay in CHow toDelay in C Set Up OpenGL GLFW GLEW GLM on a Project with Visual StudioHow toSet Up OpenGL GLFW GLEW GLM on a Project with Visual Studio Set Up OpenGL‐GLFW‐GLAD on a Project with Visual StudioHow toSet Up OpenGL‐GLFW‐GLAD on a Project with Visual Studio Print in C2 Easy Ways to Print in C and C++ Programming Start Learning C Programming in Turbo C++ IDEHow toStart Learning C Programming in Turbo C++ IDE Get Color in C ProgramHow toGet Color in C Program Set Up SFML in a Project on Visual StudioHow toSet Up SFML in a Project on Visual Studio Run CUDA C or C++ on Jupyter (Google Colab)How toRun CUDA C or C++ on Jupyter (Google Colab) Set Up an OpenGL FreeGLUT GLEW Template Project in Visual StudioHow toSet Up an OpenGL FreeGLUT GLEW Template Project in Visual Studio Advertisement

References

  1. https://packages.ubuntu.com/focal/build-essential
  2. https://docs.fedoraproject.org/en-US/quick-docs/dnf/
  3. https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  4. https://learn.microsoft.com/en-us/windows/wsl/install
  5. https://learn.microsoft.com/en-us/windows/wsl/install
  6. https://code.visualstudio.com/docs/cpp/config-wsl
  7. https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
  8. https://osdn.net/projects/mingw
  9. https://osdn.net/projects/mingw/
More References (2)
  1. https://code.visualstudio.com/docs/cpp/config-mingw
  2. https://geekthis.net/post/mingw-fix-permission-denied-ld-and-error/

About This Article

Nicole Levine, MFA Written by: Nicole Levine, MFA wikiHow Technology Writer This article was co-authored by wikiHow staff writer, Nicole Levine, MFA. Nicole Levine is a Technology Writer and Editor for wikiHow. She has more than 20 years of experience creating technical documentation and leading support teams at major web hosting and software companies. Nicole also holds an MFA in Creative Writing from Portland State University and teaches composition, fiction-writing, and zine-making at various institutions. This article has been viewed 1,176,159 times. How helpful is this? Co-authors: 37 Updated: March 13, 2024 Views: 1,176,159 Categories: C Programming Languages Article SummaryX

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. Did this summary help you?YesNo

In other languages Spanish Italian Portuguese Russian German Indonesian French Thai Dutch Arabic Vietnamese Chinese Turkish Korean Hindi Japanese
  • Print
  • Send fan mail to authors
Thanks to all authors for creating a page that has been read 1,176,159 times.

Is this article up to date?

Yes No Advertisement Cookies make wikiHow better. By continuing to use our site, you agree to our cookie policy. Nicole Levine, MFA Written by: Nicole Levine, MFA wikiHow Technology Writer Click a star to vote % of people told us that this article helped them. Co-authors: 37 Updated: March 13, 2024 Views: 1,176,159

Quizzes

Do I Have a Dirty Mind QuizDo I Have a Dirty Mind QuizTake QuizAm I a Good Kisser QuizAm I a Good Kisser QuizTake QuizRizz Game: Test Your RizzRizz Game: Test Your RizzTake QuizWhat's Your Red Flag QuizWhat's Your Red Flag QuizTake QuizAm I Smart QuizAm I Smart QuizTake QuizHow Insecure Am I QuizHow Insecure Am I QuizTake Quiz

You Might Also Like

Move from Windows to LinuxHow toMove from Windows to LinuxLearn to Program in CA Beginner's Guide to Programming in CEdit DLL Files in Visual StudioHow toEdit DLL Files in Visual StudioDelay in CHow toDelay in C

Featured Articles

21 Ways to Feel More Comfortable in Your Own Skin21 Ways to Feel More Comfortable in Your Own SkinCelebrate ChristmasHow toCelebrate ChristmasMake ShawarmaHow toMake ShawarmaMake a Chicken BurgerHow toMake a Chicken Burger Tell if a Guy Has a Crush on YouHow to Tell if a Guy Has a Crush on You Wrap a Present Like a ProHow to Wrap a Present Like a Pro

Trending Articles

 Repair Christmas LightsHow to Repair Christmas LightsiPhone Alarm Not Making Sound: Here's Why + How to Fix ItiPhone Alarm Not Making Sound: Here's Why + How to Fix ItChristmas QuizChristmas QuizWhat Is Candy Salad? Recipe Ideas, Serving Tips, & MoreWhat Is Candy Salad? Recipe Ideas, Serving Tips, & More Wrap Clothes for GiftingHow to Wrap Clothes for GiftingStop Being in Love With a Person You'll Never MeetHow toStop Being in Love With a Person You'll Never Meet

Featured Articles

 Read PalmsHow to Read PalmsBe CleanHow toBe CleanWhat Is My Aesthetic QuizWhat Is My Aesthetic QuizLace ShoesHow toLace ShoesAccept Your BodyHow toAccept Your BodyElopeHow toElope

Featured Articles

Invest in YourselfHow toInvest in Yourself See Who Views Your Facebook ProfileHow to See Who Views Your Facebook ProfileMake a MemeHow toMake a MemeImpress a Girl with WordsHow toImpress a Girl with Words50 Cute & Flirty Knock-Knock Jokes to Make Them Smile50 Cute & Flirty Knock-Knock Jokes to Make Them SmileSense of Humor: Definition, Benefits, & How to Get One YourselfSense of Humor: Definition, Benefits, & How to Get One Yourself

Watch Articles

Make Garlic OilHow toMake Garlic OilMake Crayon CandlesHow toMake Crayon CandlesFix Broken BlushHow toFix Broken BlushPrepare Lemon TeaHow toPrepare Lemon TeaCook a Basic OmeletteHow toCook a Basic OmeletteClean the Bottom of an OvenHow toClean the Bottom of an Oven

Trending Articles

Spend One's Holidays Without Getting BoredHow toSpend One's Holidays Without Getting BoredCope when You Hate Your HairHow toCope when You Hate Your HairCome Up with Good ComebacksHow toCome Up with Good ComebacksBe Cool and Popular in Sixth GradeHow toBe Cool and Popular in Sixth Grade Give Your Hair Volume (for Men)How to Give Your Hair Volume (for Men)What to Say to Keep a Conversation Going Over TextWhat to Say to Keep a Conversation Going Over Text

Quizzes

What Disney Princess Am I QuizWhat Disney Princess Am I QuizTake QuizDo I Have a Phobia QuizDo I Have a Phobia QuizTake QuizGuess My Age QuizGuess My Age QuizTake QuizAm I a Genius QuizAm I a Genius QuizTake QuizDepression TestDepression TestTake QuizWhat Human Emotion Am I QuizWhat Human Emotion Am I QuizTake Quiz wikiHow
  • Categories
  • Computers and Electronics
  • Software
  • Programming
  • C Programming Languages
wikiHow Newsletter You're all set! Helpful how-tos delivered toyour inbox every week! Sign me up! By signing up you are agreeing to receive emails according to our privacy policy.
  • Home
  • About wikiHow
  • Experts
  • Jobs
  • Contact Us
  • Site Map
  • Terms of Use
  • Privacy Policy
  • Do Not Sell or Share My Info
  • Not Selling Info
  • Contribute

Follow Us

×

wikiHow Tech Help:

Tech troubles got you down? We've got the tips you need

Subscribe You're all set! X --627

Từ khóa » Câu Lệnh Gcc