On Linux, gcc is install by default and you can start to compile and execute C language code almost right away!
That’s not the case on Windows…
I have been struggling with how to do the same thing on Windows 10 for some time, but I finally succeeded
Here’s a video in which I explain the procedure:
Note: The procedure I describe here will not only allow you to install gcc, but also g++, gfortran, and many other useful tools that you usually only get in linux OS (like ls or grep)
1- What I want to teach you here
If you want a simple way to create programs based on C, C++, Fortran, etc… you will have to install a software with can compile the code you write into code that the machine understands
Such program is called a “compiler”
The most well know compiler for C language is the GNU compiler called GCC.
There are actually several compilers which go together like a suite:
- for C++, you have the compiler g++
- for Fortran, the compiler is called gfortran
Why using the gcc compiler?
If you are on Windows, you probably heard about software such as visual studio from Microsoft which also you to write and compile C and C++ code as well.
That’s true, and it’s totally fine if you want to use it…
I am personally not such a big fan of Visual Studio because of the following reasons:
- It is huge to download and install (between 10 and 20 GB if I remember)
- It’s a big pain to set up the first time
- Building even a very simple project takes much more time than it should
Because of that, I find that I kind of takes out the fun of programming and seeing immediately the result of what you did…
Now, for large pieces of software, visual studio is probably the way to go
2- How to use the gcc compiler?
Once installed on your machine, gcc is just a piece of cake to use to compile a simple c language code
Step 1: Write your c code
For example, let’s take this Hello World example
#include #include int main() { printf("Hello World!/n"); return 0; }
Step 2: Compile using gcc
Write the following line of code into your terminal:
gcc helloworld.c -o helloworld
This will generate an executable binary file called helloworld.exe which can be used to run your code
Step 3: Execute your code
Just go into the directory where your executable is and run it by writing “helloworld.exe”
2- The principle
Now that you understand how it can be easy to start with writing C code, let’s talk about the difficult part…
How to actually install gcc on Windows…
The reason it is easy to install compilers and other kind of open source software on linux is because you have a very powerful command line terminal called bash which handles the installation, uninstallation and upgrade of software using a software called “apt” on debian-based linux systems.
On Windows, if you want to have gcc (and the other compilers too), you have to download the source code and find a way to compile it…
For that, you will need to install first other software called pre-requisites which will also need to be compiled, etc…
You can spend several days trying to do that if you are really motivated
I will present you another way much simplier!
For that I will have to install a software called MSYS2, which is an “easy” way to give some linux feel to your Windows PC and able it to install programs almost as easily as in linux…
You will be able to install gcc just with one command line like this:
Excited?
Let’s go
3- Installing MSYS2 on your PC
What is MSYS2?
According to their website, MSYS2 is a software distribution and building platform for Windows
The Website: https://www.msys2.org/
Installing it is actually a peace of cake
Just download the correct installer and launch it with admin rights and you are good to go
Expected something more complex?
Sorry about that
Once you installed it, you will see 3 programs appear in your list of programs:
- MSYS2 MinGW 32-bit
- MSYS2 MinGW 64-bit
- MSYS2 MSYS
Run the 3rd one MSYS2 MSYS to open a special command window in which you can input commands and start to install programs
The package management system which is equivalent to apt-get on linux is called pacman
When you install MSYS2 for the first time, you have to update pacman using the following command:
pacman -Syu
4- Installing GCC and other development tools with the pacman package management system
Then you can start to install gcc and other developer tools using pacman like this:
# Install make, autoconf, etc to C:\msys64\usr\bin pacman -S base-devel gcc vim cmake
This command will install first a set of development software included in a package called base-devel and then it will install gcc, vim and cmake
vim is optional, but always good to have ;-)
(You can also install emacs very easily in the same way)
In the package base-devel, you have 53 useful developement tools that can be installed:
# pacman -S base-devel 1) asciidoc 2) autoconf 3) autoconf2.13 4) autogen 5) automake-wrapper 6) automake1.10 7) automake1.11 8) automake1.12 9) automake1.13 10) automake1.14 11) automake1.15 12) automake1.16 13) automake1.6 14) automake1.7 15) automake1.8 16) automake1.9 17) bison 18) diffstat 19) diffutils 20) dos2unix 21) file 22) flex 23) gawk 24) gdb 25) gettext 26) gettext-devel 27) gperf 28) grep 29) groff 30) help2man 31) intltool 32) lemon 33) libtool 34) libunrar 35) libunrar-devel 36) m4 37) make 38) man-db 39) pacman 40) pactoys-git 41) patch 42) patchutils 43) perl 44) pkg-config 45) pkgfile 46) quilt 47) rcs 48) scons 49) sed 50) swig 51) texinfo 52) texinfo-tex 53) ttyrec # pacman -S gcc binutils-2.30-1 isl-0.19-1 mpc-1.1.0-1 msys2-runtime-devel-2.11.1-2 msys2-w32api-headers-6.0.0.5223.7f9d8753-1 msys2-w32api-runtime-6.0.0.5223.7f9d8753-1 windows-default-manifest-6.4-1 gcc-7.3.0-3
5- Last Important Step: Add Executables to your Windows PATH
Now you are able to install all those packages easily, but how to access them through the normal windows command line?
For that, you have to add the two following directories to your PATH:
C:\msys64\mingw64\bin C:\msys64\usr\bin
To add anything to your PATH in Windows 10:
1- Search “Advanced System Settings” in the search bar
2- Open the “environment variables” window, click on the PATH variable and click on “Edit”
3- Click on the “New” button and add the 2 following directories to your PATH
Important Note: This adds a lot of executables to your path which might conflict with other applications. The usr\bin\ directory contains the whole slew of executables listed above. There is a lot of unnecessary stuff in that directory.
4- Click on OK on all the windows and open a new Command Window and you will be able to use gcc into your windows command prompt!
That’s all for today, hope you understand better now how to install and use GCC and other linux tools on windows!
I will write new articles soon about how to develop simple programs in C.
Let me know through the comments if there is something special that you want to understand!
–Cyprien
PS: If you like what I write, subscribe to the newsletter and help me to spread the knowledge by sharing this article! We all win by learning from each other and making the engineering knowledge more accessible :-) THANK YOU!
Mickey White says
gcc -v give me version and is good, but gfortran -v not found gfortran is not found
Cyprien says
Hi Mickey,
Check this page: https://github.com/gher-ulg/Documentation/wiki/Windows-MSYS2
Once you have MSYS2 installed, you can search for packages withe the command:
pacman -Ss gfortran
It will give you something like that:
mingw32/mingw-w64-i686-gcc-libgfortran 7.4.0-1 (mingw-w64-i686-toolchain)
GNU Compiler Collection (libgfortran) for MinGW-w64
mingw64/mingw-w64-x86_64-gcc-libgfortran 8.2.1+20181214-1 (mingw-w64-x86_64-toolchain)
GNU Compiler Collection (libgfortran) for MinGW-w64
Then choose the one you want and install it with (for example):
pacman -S mingw64/mingw-w64-x86_64-gcc-fortran
Mickey White says
Thanks, That works!
Mickey
Sam I says
I downloaded C alright, and could use it when I saved the c source code file in the directory within the path (msys\usr\bin) but when I tried to run a C source code saved on my desktop I received the following error. How to I use gcc beyond the
C:\Users\Sam\Desktop>gcc test.c -o test
‘gcc’ is not recognized as an internal or external command,
operable program or batch file.
When I try to compile in the path, I just receive a compiling error saying printf isn’t declared. Thanks for the help
Cyprien says
Did you actually installed MSYS2?
Did you run MSYS2 terminal command (if you installed MSYS2, just search for MSYS2 in the search bow and click on the first recommanded program) and entered “pacman -S base-devel gcc vim cmake” to install gcc?
Did you saw the install process and did it installed it without any problem?
If you go into the msys\usr\bin\ folder, do you have the gcc executable inside it?
Sam I says
I got it up and running now. My best guess is did not set up my path correctly, so I set that up again and got it working alright. Thank you so much for all the help
Abdulmalik says
Please how did you set up the path in the correct way? I am having issues with this “path thing”
Cyprien says
It’s explained in details in part 5… what doesn’t work for you?
gary knott says
Dear Cyprien, Thanks for your notes on gcc under MSYS.
(1) I want to build a C-program that (1) uses Windows graphics api’s,
(2) the .exe runs on win10 without MSYS – so it can be
given to any win10 user!
How do I build a 64-bit program, and how do i build a 32-bit program, on
either 32 or 64 bit machine I set-up gcc on?
And (3) I’d like to find a manual that tells me about all the
functions defined in header files that i can use in
writing my program. — i.e. I want documentation.
Can you answer these questions?
(P.S>, if you like math, as you seem to, you
may enjoy http://www.civilized.com — that’s the program i want to
build.
Cyprien says
Hi Gary,
What I wrote on this page is mainly for people who want to start learning to compile code with GCC without the hassle generally brought by Microsoft Visual Studio and all or other similar Mammoths…
If you want to build a program for cross-platform and 32/64 bits and for Windows… I think you unfortunately won’t escape the Microsoft Visual Studio full install and you will need to setup correctly your dev environment.
I suppose there might by some ways to do it with GCC to, but I am not an expert in that, so I can’t tell you more.
Good luck on your project, looks like a lot of fun!
igor.shaula says
thank you for letting me know about MSYS – that’s way better than good old MinGW with x86 version & one util only. with MSYS we get plenty of Linux tools -and that’s awesome!
ARUN NATH G says
This page was very useful for me thank you
Iasmin Silva says
I installed it and all, but if I don’t want to integrate with my prompt from windows, how do I use it to write and compile code? I’m new to this coding stuff sorry if its a basic question.
Cyprien says
Write your C language code in a text file “helloworld.c”, save it. Then use GCC to compile it: Open the terminal, enter the command “gcc helloworld,c -o helloworld”.
That’s the way it works!
If you want a more graphical and integrated way, I suggest you install “Microsoft Visual C++” with the C language add-on and packages.
Marco Rossi says
Hello cyprien, i followed your guide and everything went well, so now i’m able to compile c programs from my command prompt. I have a problem though: sometimes i get this error when i try to execute my program:
0 [] filename1056 cygwin_exception::open_stackdumpfile: Dumping stack trace to filename.exe.stackdump
The number 1056 changes everytime i execute the program. I think it has something to do with the useable space somewhere, because i get the error if i declare a long double array, but if the array is shorter i don’t get the error.
Can you please help me?
Projjal Moitra says
Thank you so much. It works! :D
jr duj says
Hello,
Thanks for the tuto, this look very helpful. I do have some issue running the gcc though…
I’ve installed everything in admin mode (I’m not logged as an admin on my windows), and the gcc compiler is recognized in the comand prompt, when open in admin mode.
If I open the comand prompt as normal mode (no admin), the gcc compiler is not recognize.
I need it to run codes from python, that required the gcc, which are not working like this. I’m guessing this should work if I can get the gcc compiler to be running in the non-admin comand prompt.
Thanks !
Cyprien says
Hi Remi,
That’s weird that you have to have gcc to run python code… python is a script language.
gcc is a compiler for language c code.
I suspect that you are trying to compile from source a software when there is an easier way available somewhere else (Try to search for the binaries of the software you want to install)
What may also happen is that you are trying to run the windows version of Python.
Gcc installed here is available through MSYS2, which is more or less like an emulator.
You can use it as a stand alone software to compile c language, but other applications might not necessarily work.
jr duj says
Hi,
Yes I’m actually running a python through windows.
But the code I want to use is calling c code as well. I don’t know the detail of it, but it looks like the gcc is required. I was hoping this could gap the bridge ! (the code has been initially written in Linux).
Anyway, thanks a lot for the prompt reply !
Dan LL says
I appreciate your work sir , but it took me most of a day to get the thing to work .. here are a few tiny adjustments to your instructions will keep the next guy from struggling 8 hours as I did.
Still your instructions are the best I’ve found & I’m thankful.
The single biggest hang up I had is my last comment …
1.) Notice our teacher says that after Msys2msys you must update it. You must type the update command in the Msys2 window not in a windows terminal ) You’ll see the tile in your start directory & search for GCC (thinking its been installed) but it isn’t. It may have things in there besides a mere update.
But when its done .. you still not have gcc installer. That comes later.
The following is not one line of instructions .. these are 2 separate lines of instructs NOT A WRAPPED line.
Enter the 1st line, then wait till its done. then Enter the 2nd line
line1: # Install make, autoconf, etc to C:\msys64\usr\bin
Line2: pacman -S base-devel gcc vim cmake
Dan LL says
Really good. Thanks for that. I need a make file procedure. & it looks like I’ll find one in pacman .. though I haven’t looked yet. If you have a srhot tutorial on tht please send it. If not I’ll struggle .. but I’ll get there
Dan LL says
Never mind .. I found the tutorial for same .. Think you -Dan-