Fork me on GitHub

Simple Gnu C Compiler With Nix Shell On Windows

By Damien · 2015-06-25 · c quicktip

alt text

This 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 http://sourceforge.net/projects/mingw/files/latest/download?source=files

alt text

Execute the MinGW installer

alt text

in admin mode

alt text

Follow the installer steps

alt text

alt text

alt text

Then close the window

alt text

A new winows appears : the installer

alt text

select packages to install :

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

alt text

alt text

Then click on Installation menu, update catalogue

alt text

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

alt text

Click on Apply

alt text

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

alt text

After, close the installer

alt text

Explorer your C: harddrive. A new MinGW folder appears

alt text

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

alt text

A new NIX shell appears.

alt text

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

alt text

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

alt text

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

alt text

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

alt text

Open .profile with your text editor and add this :

.profile

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

alt text

Save it. And start msys.bat again

alt text

At NIX shell, type the ls command.

 

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

alt text

Close this windows.

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

alt text

alt text

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);
}

alt text

alt text

Launch msys.bat

alt text

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

gcc test.c

alt text

Execute the generated executable with : a NIX command :

./a.exe

alt text

or a Windows click on the .exe

alt text

or a windows command line

a.exe

alt text

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 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 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

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

Origin

https://damienfremont.com/2015/06/25/simple-gnu-c-compiler-with-nix-shell-on-windows/

Hi, I'm Damien

Software Developer

LinkedIn GitHub Twitter

Founder of this blog, love Java and Open Source stuff. Follow him on Twitter.