This article shows you how to create a simple Linux environment on Windows where you can practice programming using the C language. This tutorial will be based around the Ubuntu Linux Terminal used at MSU-IIT CSC 101 class under Prof Maulana. The nearest BASH terminal I could find that can easily simulate the one on Ubuntu Linux is Cygwin. Cygwin has a lot of programs that can be attached to it including nano and gcc.
Cygwin is not a Linux implementation but instead it's a simple emulator of a Linux Terminal under Windows. In addition, Cygwin works for Windows XP, Windows Vista and Windows 7. If you are a Mac user then there should be no problem since Macs are Unix based and have its own Unix terminal that is very similar to Ubuntu Linux. This however will be kept as simple as possible so I'll be using a lot of screenshots to keep things as graphical as possible.
And finally, if you are done reading this article you can download or view online this E-book on C Programming that I uploaded as an attachment to this post which I also got from Mole. In order to open the file and read the E-Book, make sure you have Adobe Acrobat Reader.
1. Let us begin by downloading Cygwin from this link or you can go to this link and find "Install or update now!" In the lower part of the page.
2. After downloading the setup.exe file, double click it to run the installer.
3. Then click next.
4. Choose Install from the Internet then click next.
5. Install it at the default location which is at "C:\Cygwin".
6. Choose the directory where you want to store the downloaded installation files. Remember this directory because it is important especially when you decide to reinstall your package to avoid downloading installation files again.
7. In most cases your internet connection will be direct instead of "HTTP/FTP Proxy". So use the selected default "Direct Connection".
8. After that it will load the list of installation files for a while. Wait.
9. After that you can finally choose which packages to include in your Cygwin installation.
10. First go to the 'Devel' section.
11. Then choose the C and C++ compilers by clicking the little refresh symbols in the new column. The most recent versions of these libraries will appear and there will be an x mark on the boxes at the Bin ('B') column to indicate that you are including them for the download.
12. Then let's go to the editor section to include the Linux editors.
13. Choose the editor of your preference. My personal preferences are Emacs and Vim, you may also download Nano.
14. After clicking next, Cygwin Setup will inform you that it will also download several other library dependencies. Make sure the 'Select required packages' is checked, then click next.
15. After clicking next, the downloading of the required packages and installation of Cygwin will follow.
16. After the download and installation of Cygwin has been completed, click next. You may also check the options on this last prompt depending on your preferences.
17. After the installation you can find the Cygwin shortcut at your desktop or your Program Files. You can finally test your installation by running Cygwin and issuing the following commands. Refer to the following image to see what should be the result.
mkdir Exercises
ls
cd Exercises
touch MyFirstProgram.c
nano MyFirstProgram.c
18. After typing "nano MyFirstProgram.c", the Nano program will run under Cygwin.
19. Encode the following code (or as shown in the previous image) then press 'Ctrl+O'. It will prompt for the file name, but instead use the default as supplied, hence press 'Enter' directly.
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
20. After saving the file (Ctrl+O or WriteOut), you'll be sent back to the Terminal. Issue the following commands to compile and test your first c program in Linux as simulated by Cygwin on Windows. Refer to the following image to see how it will look like in the end.
gcc MyFirstProgram.c -o MyFirstProgram.exe
./MyFirstProgram.exe
In this tutorial we are using Cygwin to simulate the Linux terminal on Windows. This is very handy if you are testing a certain source code if it will compile in Linux and you only have a Windows PC available. There are however a lot of other important things that you should know about regarding this setup. But we have to understand that Cygwin can only provide the most basic and standard Linux capabilities on your Windows PC and it does not provide the entire breadth of feature you can find in a full Linux installation like Ubuntu, Fedora, Red Hat Linux or others. But in the end, Cygwin can pretty much do a lot of the things a standard Linux terminal can. I hope this simple tutorial will serve as a springboard for the readers interest in programming.
Miscellaneous
The following are simple facts about Cygwin that may help you:
ctrl+L
to clear the command prompt.The following are handy commands used in Linux:
cd
Changes directories. As you can see, each slash (/) indicates another sub-directory. cd Exercises
Moves down from your current directory into the Exercises sub-directorycd ..
Moves up one directory (yes, include the two little dots) cd /home/particle/muondata
You can also move directly into directories Moves from ANY directory into the muondatasub-directory of your home directory.cd ~
Takes you back to your home directory (/home/particle)mkdir dirName
Creates a directory with name dirName.mkdir temp
Creates the directory temp.rmdir dirName
Removes a directory dirName.rmdir temp
Removes the directory temp.ls
Looking at or Finding your Files (terminal mode) Lists filesls al
If you add -al after ls it will give more details for each file. Such as, size, permissions, owners, dates etc. You'll see a huge list of files that you can't see with the 'ls' command alone and lots of details.ls -al |more
If you see such a long list of files that they scroll off the terminal screen, one way to solve the problem is to use: Shows one screen of file names at a time.less data1
Dumps the contents of the data1 file to your screen with a pause at each line so you don't miss any contents as they scroll. You may move through the file using page up, page down, home and end keys. When done with less you use the q key to get back to the main terminal.whereis data1
Shows you the location of the data1 file.rm data1
Altering your Files. Deletes the file data1 in the current directory. rm -i muon*
Removes all of your muon data files (careful!! rm * will remove ALL your files) The "-i" makes the computer prompt before removing each file. If you really want to work without a net, omit the "-i".cp data1 newdata/
will copy the file data1 to the directory newdata (assuming it has already been created)mv data1 newdata/
moves the file data1 to the folder newdata and deletes the old one.For more information: