Simple GNU C Compiler With NIX Shell on windows

c-logoThis tutorial is intended for developers who want to start developing in C on Windows, without having to use a heavy software like VisualStudio. And having a Linux like shell of course.

How to

Download MinGW for Windows at:

http://sourceforge.net/projects/mingw/files/latest/download?source=files

ScreenShot001

Execute the MinGW installer

ScreenShot002

in admin mode

ScreenShot003

Follow the installer steps

ScreenShot004

ScreenShot005

ScreenShot007

Then close the window

ScreenShot008

A new winows appears : the installer

ScreenShot001

select packages to install :

  • dev-tools
  • base (c compiler)
  • g++ (c++ compiler)
  • msys (NIX shell)

ScreenShot002

ScreenShot003

Then click on Installation menu, update catalogue

ScreenShot004

A new window appears with some gibberish text. Click on Review

ScreenShot005

Click on Apply

ScreenShot006

Then wait for the end of the download (30 secondes)

ScreenShot007

After, close the installer

ScreenShot003

Explorer your C: harddrive. A new MinGW folder appears

ScreenShot004

Click on C:\MinGW\msys\1.0\msys.bat

ScreenShot005

A new NIX shell appears.

ScreenShot006

Create a .profile file inside (on Windows it’s not possible to create a file starting with “.”, so copy a file from my github or create it with touch command with the NIX shell)

touch .profile

ScreenShot002

Close it. It’s just for the creation of the default user home folder.

ScreenShot008

A new home folder appears at C:\MinGW\msys\1.0\home\

ScreenShot009

Open it. It contains your home folder (in my case: C:\MinGW\msys\1.0\home\Damien)

ScreenShot010

Open .profile with your text editor and add this :

.profile

# Cd to my windows home:
cd /c/Users/Damien

ScreenShot011

Save it. And start msys.bat again

ScreenShot005

At NIX shell, type the ls command.

ls

The result is the content of your Windows user folder, instead of the NIX C:\MinGW\msys\1.0\home\***

ScreenShot002

Close this windows.

Then create a shortcut of msys.bat to your desktop or set it in your Windows path env.

ScreenShot001

ScreenShot001

Your environnement is ready!

Demo

Create a c source code file in your user folder with your text editor

test.c

#include <stdio.h>

int main(){
	printf("Hello World!");
	return(0);
}

ScreenShot002

ScreenShot001

Launch msys.bat

ScreenShot001

A new NIX shell appears. Type the compile command gcc on your new source file.

gcc test.c

ScreenShot001

Execute the generated executable with :
a NIX command :

./a.exe

ScreenShot003

or a Windows click on the .exe

ScreenShot002

or a windows command line

a.exe

ScreenShot004

Conclusion

MinGW is a simple and friendly tool for those who seek to program on a Linux like shell on Windows.

…BUT the MinGW documentation is very harsh with newbies, installer steps are very strange for a Windows user and the downloading package part required a lot of time (not easy in offline mode).

It’s not a turnkey solution, but it does the job.

Source

https://github.com/DamienFremont/blog/tree/master/20150625-c_compiler_win_shell

References

https://assos.centrale-marseille.fr/ginfo/tutoriels/ma%C3%AEtrisez-la-compilation-cc-sous-windows

http://stackoverflow.com/questions/7000524/changing-mingw-startup-directory-or-creating-mingw-symlinks

http://stackoverflow.com/questions/7000524/changing-mingw-startup-directory-or-creating-mingw-symlinks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s