Install gcc Compiler on Windows, gcc in Cygwin, Codeblocks, Compile, Run
In order to write C programs on windows, Let us install Cygwin, GCC compiler and code::blocks.
For this tutorial, we are going to set the environment as shown below
Now, let us do this step by step.
Download Cygwin
For this tutorial, We are going to install Cygwin. Cygwin setup-x86_64.exe can be downloaded from https://www.cygwin.com/
Install Cygwin Packages
Run setup-x86_64.exe . Please follow the steps in the installation wizard
Please follow the steps to choose the packages wget, make, gcc-g++, debug, devel, libs, and utils for installation.
You can either click on setup-x86_64.exe using the UI to choose the utilities mentioned above or use the command line:
setup-x86_64.exe -q -P wget,gcc-g++,make,diffutils,libmpfr-devel,libgmp-devel,libmpc-devel
Download GCC
Download the GCC source code from one of the mirror sites listed in:
https://gcc.gnu.org/mirrors.html
Choose a mirror
Install GCC
Copy the file to a specific folder and Extract the gcc using the following command
tar xzf gcc-9.3.0.tar.gz
Once the file is extracted, a new folder named gcc-9.3.0 will be created. Execute the following commands one by one
cd gcc-9.3.0 ./contrib/download_prerequisites
Once the download prerequisites is complete, please create a subdirectory objdir and use cd to go into the directory
cd .. mkdir objdir cd objdir
Now, Execute the following command one by one to choose the languages to be supported by gcc. As shown below, 'configure' is used to set the configurations before doing the build. Please note that the command PWD points to the present working directory.
$PWD/../gcc-9.3.0/configure --prefix=$HOME/GCC-9.3.0 --enable-languages=c,c++
Build the source using the 'make' command. You can use make -j followed by
make -j4
make -j4 might take hours depending on the speed of the machine.
Sometimes, you may face issues while running the command mentioned above. In such cases,instead of make -j4, you might need to use make -j2 on older machines. Alternatively, On newer machines, you might need to use make -j8
the number used (j2, j4 or j8) depends on the number of threads that can be executed on the machine
Once the make command has completed successfully, execute the 'make install' command to copy the compiled code to various locations
make install
Set the Path
Set the path of the cygwin installation in PATH variable. If you have installed cygwin in the location "C:\cygwin64", then you can set PATH using the command prompt:
SET PATH=%PATH%;"C:\cygwin64\bin"
Install the IDE Code::blocks
You can install any of the popular IDEs, some of those being: Eclipse for C/C++, Code::Blocks, NetBeans, Geany, Visual Studio, Code. It is not mandatory to have an IDE. You can also use simple editors available for your OS. On Windows: You can use Notepad. Or You can download and use editors like Textpad.
If you are managing a large project in C, IDE would be of good help.
For this tutorial, we will be using Code::blocks. You can download Code::Blocks from:
http://www.codeblocks.org/downloads/binaries
Get the IDE ready
Once the IDE is installed, Open it and Go to Settings menu and click on Compiler
In the Compiler window, Choose GCC compiler. Click on 'Toolchaining' tab and type cygwin Installation bin location. For this tutorial, it is "C:\Cygwin\bin" and click on Ok.
Using Code::blocks, Create New File to Write a C Program
In Code:Blocks, on the left "Management" tab, click on Files tab and choose a Drive. Say "E:". Click on the drive name, right click and choose 'New file'.
In the dialog box that appears, type File name say 'Program1.c' and click ok. You can see the file created on the left side.
Click on the new file created and type your C program on the right editor. You can write the code with an error, so as to see how to handle errors
Using Code::blocks, Compile C Program
Click on 'compile' as shown below from the build menu
Compilation errors are display at the bottom.
Click on the error displayed and correct the issues in the editor and Save. Click on 'compile the file' from build menu.
Using Code:blocks, Execute C Program
Once code is compiled without any errors, you can find two additional files created with the same file name but with extensions '.o' for object file and '.exe' for executable file. In this case, it would be 'Program1.o' and 'Program1.exe'
Click to Zoom
That is All, You did it
Cygwin alternatives
For other GCC alternatives to Cygwin on various operating variants, please refer to https://gcc.gnu.org/install/binaries.html
C Compiler alternatives to GCC
There are too many C compilers in the market. In this tutorial, we will installed GCC compiler - one of the popular compilers for C and C++ programs/projects. Commonly used C Compilers are:
- GNU GCC - Popular C++ compiler
- Portable C
- Intel C++ compiler
- Microsoft VC++ - only for windows
- Borland C++
- Turbo C++ - not released after 1996