Cygwin
How To Install and Get Started
Cygwin = GNU + Cygnus + Windows. The mother site for Cygwin is www.cygwin.com.
"Cygwin is a Open-source Linux-like environment for Windows. It consists of two parts:
- A DLL (
cygwin1.dll) which acts as a Linux API emulation layer providing substantial Linux API functionality. - A collection of tools which provide Linux look and feel." (extracted from www.cygwin.com)
How to Install Cygwin
Step 1: Download Setup
Download the setup program "setup.exe" from www.cygwin.com.
Step 2: Run Setup to Select, Download & Install Cygwin Packages
Run "setup.exe" ⇒ Install from Internet ⇒ select a directory (avoid installing in "Program Files" because of that "blank" character) ⇒ choose "Local Package Directory" which saves the downloaded installation files ⇒ select your Internet proxy setting ⇒ choose a download mirror site.
(If "choose download site" box is empty, add "ftp://mirror.averse.net/pub/cygwin" in the "User URL". You can find all the mirror sites from http://cygwin.com/mirrors.lst.)
Select the packages that you wish to install.
Important: For programmers, you certainly need to open the "Devel" (Development) category and select "gcc", "g++", "gdb", "make", and others, which are not part of the default selection.
Browse thru all the categories. Complete the installation process. You can always re-run "setup.exe" to install additional packages later.
Step 3: PATH
Include the Cygwin Binary directory (bin) in the PATH environment variable.
Suppose that your Cygwin is installed in directory "c:\cygwin". From "Control Panel" ⇒ System ⇒ (Vista only) Advanced System Settings ⇒ Advanced ⇒ Environment Variables ⇒ System Variables ⇒ Select variable named "PATH" ⇒ Edit ⇒ Add "c:\cygwin\bin;" in front of the existing PATH entry. Note that the semi-colon serves as the directory separator to separate Cygwin from the rest of directory paths.
Step 4: Verify Cygwin
Start the Cygwin Command shell (bash or sh) by running "cygwin.bat". You shall see the command prompt "$". You may need to create the users' group and password files by running the following commands:
$ mkpasswd –l > /etc/passwd $ mkgroup –l > /etc/group
Try out some Unix commands (you need to read a Unix book - there is no short-cut in learning), e.g.,
$ ls $ dir $ pwd $ cd newdir $ cd .. $ cd / |
List the current directory Similar to "ls" Print (or display) the current working directory Change current working directory to newdir Change current working directory to its "parent" directory Change current working directory to its "root" directory |
Need help? Try:
$ man command $ man –k keyword $ man –f command $ whatis command $ info command $ help |
Display the manual pages for command Display commands containing keyword Display a brief description of command Same as "man –f command" Display the information pages for command Display the help menu |
On bash shell, after setting the directory to the the "root" (via "cd /"), you could find a directory called "cygdrive" (via "ls" or "dir"), where all the hard disks are mounted. "cd cygdrive" and "ls" lists all the hard disks, e.g., "c", "d", etc.
You may mount your C drive ("c:") as "/c" instead of the default "/cygdrive/c" via this command:
$ mount c: \c $ mount |
Mount Drive "C:" as "\c" Display the current mounts |
You could invoke the Cygwin programs and utilities via the Windows' Command Prompt ("cmd.exe") instead of bash shell (provided the PATH is set properly), e.g.,
> ls -alR > man gcc
Compiling a C/C++ Program in Cygwin
Use a text editor (such as TextPad, NotePad) to create the following C/C++ source file:
/* C Program - "Hello.c" */
#include <stdio.h>
int main() {
printf("Hello, world!");
}
// C++ program - "Hello.cpp"
#include <iostream>
using namespace std;
int main() {
cout << "Hello, world!" << endl;
}
Start a Command Shell ("cmd") (or bash shell), set the current working directory to where the source file resides, and issue the commands:
> gcc -o Hello.exe Hello.c > g++ -o Hello.exe Hello.cpp > Hello |
Compile "Hello.c", option "-o" specifies the output filename Compile "Hello.cpp" Run "Hello.exe" |
To compile and run under the Cygwin's bash shell, use the commands:
$ gcc -o Hello Hello.c $ g++ -o Hello Hello.cpp $ ./Hello |
Compile "Hello.c", option "-o" specifies the output filename Compile "Hello.cpp" Run "Hello.exe" |
Note that:
- Unix executable files need not carry the extension of "
.exe", but possess "executable" attribute. - In Unix, the current working directory "
." is not searched, unless it is included in the PATH. Hence, "./Hello" is used to execute "Hello" from the current directory.
The command-line options for gcc include:
gcc [-c|-S|-E] [-std=<standard>] [-g] [-pg] [-O<level>] [-W<warn>...] [-pedantic] [-I<dir>...] [-L<dir>...] [-D<macro[=defn]>...] [-U<macro>] [-f<option>...] [-m<machine-option>...] [-o <outfile>] <infile>...
Commonly-used gcc compiler options (case sensitive) are:
-o <outfile>: Specifies output filename, default is "a.exe".-c: Compile only, without linking.-I<dir>: Specifies the INCLUDE directories for searching the header files.-L<dir>: Specifies the LIB directory for searching the libraries (for linking).-l<lib-name>: Link with this library.-D<macro>: Define the macro.-U<marco>to un-define the marco.-Wall: Show all warning messages.
gcc uses the following environment variables:
PATH:CPATH: Define search paths for C/C++ headers (after paths specified in option-I<dir>).C_INCLUDE_PATHandCPLUS_INCLUDE_PATHcan be used to specify C and C++ headers if the particular language was indicated in preprocessing.LIBRARY_PATH: Define search path for link libraries (after paths specified in option-L<dir>).
REFERENCES & RESOURCES
- Cygwin Mother Site @ www.cygwin.com
- Cygwin Documentation
Latest version tested: Cygwin DLL 1.5
Last modified: September 5, 2007