Getting Started With Cygwin
Getting Started With Cygwin
STARTED WITH
CYGWIN
Authors: Timothy Puffer and Carl Simonson
TABLE OF CONTENTS
TABLE OF CONTENTS 3
INTRODUCTION 5
WHAT IS CYGWIN? 5
WHO IS THIS DOCUMENT FOR? 5
DOCUMENT CONVENTIONS 5
SETTING UP CYGWIN 7
DOWNLOADING CYGWIN SETUP 7
INSTALLING CYGWIN 7
Choosing Your First Set of Packages 11
Finishing the Installation 12
ADDING AND REMOVING PACKAGES 15
ADDING PACKAGES 15
REMOVING PACKAGES 15
UPGRADING ALL PACKAGES 16
USING VI 17
CREATING A FILE IN VI 17
Starting Vi 17
Entering text 17
Saving a file 18
EDITING A FILE WITHOUT SAVING 19
Opening and closing a file 19
CREATING YOUR FIRST C++ PROGRAM 21
CREATING A FILE FOR USE IN CYGWIN 21
Creating a file Using Windows 21
Creating a file using the Cygwin environment 22
COMPILING AND RUNNING C++ FILES 23
APPENDIX A 25
USING THE VI EDITOR 25
Vi Commands 26
3
L E S S O N 1
INTRODUCTION
What is Cygwin?
Cygwin is a Linux-like environment for Windows. It gives you programs and tools common on a
Linux environment without the cost of running a virtual machine. Because it runs natively on
Windows, you can use the Windows and Linux programs to modify the same file without having
to transfer them between the two systems.
You can find out more about Cygwin by visiting the website: http://cygwin.com.
Document Conventions
Several formatting conventions are used throughout this document to facilitate understanding of
the material. The styles are described below with an example:
Formatting Style Example
Commands (typed verbatim) vi test
File/Path Names hello.cpp
Literal text strings/Proper names Click Next
Keystrokes Esc
5
L E S S O N 2
SETTING UP CYGWIN
Downloading Cygwin Setup
To begin using Cygwin, you must first install it. To download the installer, follow these
instructions:
1. Open your browser
2. Navigate to http://cygwin.com.
3. Click on the Install Cygwin Now link on the right. The link is shown in Figure 1.
Installing Cygwin
Now that you have downloaded the installer, you must run it. The installer will prepare and set
up your computer with the Cygwin environment.
1. Run setup.exe. The installer shows up as seen in Figure 2.
7
Figure 2: The Cygwin Setup welcome screen.
2. Click Next. The Choose Installation Type dialog show as in Figure 3.
Figure 3: The Choose Installation Type dialog.
3. Choose Install from Internet if not selected.
4. Click Next.
5. At the Choose Installation Directory dialog, accept the defaults by clicking Next.
9
Figure 4: The Choose Installation Directory dialog.
6. Click Next at the Select Local Package Directory dialog.
8. At the Chose Download Site(s) dialog, pick a mirror from the Available Download Sites
list.
10
Figure 5: The Choose Download Site(s) dialog.
9. Click Next.
The Select Package” dialog appears containing a list of packages to download and install. In the
next section, you will pick a few packages to install.
Note: Don’t install all packages unless you want to! Cygwin comes with a variety of different
software. By choosing to install everything, you may have to wait a long time for all of the
packages to download and install.
Selecting GCC
To add GCC to the list of packages to install, do the following:
1. Expand the Devel item by clicking on the plus sign next to it.
2. Scroll down and look for the gcc package in the Package column.
11
3. Click on the circular arrows on the leftmost side of the gcc row as shown in Figure 6.
Selecting ViI
Adding Vi to the list is similar to the steps for gcc.
1. Scroll down to the Editors category
12
Figure 7: Cygwin Setup's helpful installation screen showing the progress of the installation.
2. Cygwin Setup starts downloading and installing the packages. Please be patient as the
installation may take several minutes. After Cygwin Setup has finished installing, the
Create Icons dialog will show up as seen in Figure 8.
13
Figure 8: The Create Icons dialog.
3. Click Finish.
4. Click OK when Cygwin Setup prompts you that the installation was complete.
You have successfully installed Cygwin on to your computer. Now go to the next lesson to learn
how to add, remove and upgrade packages in Cygwin.
14
L E S S O N 3
Adding Packages
Adding a package is similar to selecting a package as done in the first install.
1. Follow steps 1-10 in the Installing Cygwin section on page 7.
3. To help find the package you are looking for, click the View button to cycle between
views.
Note: Changing views can help you quickly find packages to install. Other useful views are the
Partial view, which shows changes you have made, and the Up To Date view, which shows the
installed packages.
Removing Packages
Removing a package is done in the Cygwin Setup program.
1. Follow steps 1-10 in the Installing Cygwin section on page 7 to get to the Select
Packages dialog.
3. Click the circular arrows in the package row until the text to the right reads Uninstall.
Figure 9 shows what the screen looks like after the gcc package has been selected for
removal.
Figure 9: The Select Packages dialog after gcc has been selected for removal.
4. Repeat the process for all packages you wish to remove.
USING VI
In this chapter you will learn the basics of the Vi editor. Vi can seem like a hard-to-use editor at
first, but is essential to learn since it is on every UNIX system.
Creating a file in Vi
To begin learning VI, you will create a file using VI, edit it, and save it.
Starting Vi
The Vi editor is accessed though the vi command. In this section you will start Vi with a blank
file name.
1. Open the Cygwin command prompt by double-clicking the Cygwin icon on your desktop.
2. Type vi test to open Vi with a blank file named test. The screen should look like Figure
10: The Vi screen..
Entering text
You will now enter some text into the test file.
1. With Vi still open, type i to enter insert mode. Unlike other editors, Vi has different
modes. The default mode called command mode allows you to type commands to save
files, perform operations on text, and other things. Insert mode allows you to enter text.
Note: If you ever get lost and don’t know what mode you are in, hit Esc to return to command
mode.
3. Hit Esc to finish typing and to enter command mode. Your screen should look like Figure
11: The Vi editor after entering some text..
Saving a file
Now you will complete your changes by saving the file.
1. Hit Esc to make sure you are in command mode.
Vi exits without saving the file. You have completed the Vi primer lesson.
19
L E S S O N 5
2. Open C:\cygwin\home. On DSU machines you must open up the DSU folder. On other
machines you will not need to do this step.
Figure 12: Windows Explorer with the Cygwin home directory in the address bar.
3. Close My computer
Note: One of the nice things about Cygwin is that it is a Unix/Linux Environment but it can read
Windows end lines. Windows and Unix/Linux store their end lines (i.e. the return character)
differently.
#include <iostream>
int main ()
{
std::cout << “Hello World!\n;
return 0;
}
Note: To users that are used to using the Windows environment, that static main entry point must
return int in Unix/Linux.
6. Save this file to the Cygwin home folder from step 1. Name the file hello1.cpp.
Note: You must manually enter the .cpp extension. If the file is .txt it will still work in fact not
change anything but it will cause confusion.
7. Open Cygwin.
8. In the command line type ls. The ls command is the command the will list all file within
the current directory. At this point you should see the file the you made in the list of files
that are in the folder. hello.cpp.
2. Type in vi hello1.cpp. The vi command opens the Vi text editor. Since the file
hello1.cpp file does not exist, Vi will create it when it is saved.
7. You should be back at the command prompt. Type ls to see if the file that you created is
there.
3. Type in the following command: g++ hello.cpp. If all runs correctly you will not receive
an error message. If it does not open up correctly refer back to the previous section a
reenter the syntax.
23
4. Type ls and note the new file.
Figure 19: Classic "Hello World" program after running compiled program.
Note: The ./ command tells Cygwin to look at the current working directory.
7. Type in the following command: g++ hello1.cpp -o hello1. The –o hello1 argument will
save the executable as hello1.exe. If you do not specify this option, you will overwrite
the a.exe file.
Figure 21: The successful run of the second "Hello World!" program.
Congratulations! You have just created, compiled and run a C++ program in Cygwin.
24
APPENDIX A
Using the Vi Editor
The Vi editor is available on almost every Unix system including Sun SOLARIS.
1. The command mode: In this mode the characters you type in are used as commands.
2. The insert mode: In this mode the characters typed in are inserted into the file as text.
You may see some lines starting with the ~ character. These are just placeholder lines that are
past the end of your file.
Screen Movement
Ctrl-U Scroll Up
Ctrl-D Scroll Down
Ctrl-F Scroll Forward
Ctrl-B Scroll Back
Line Movement
j Move down one line
k Move up one line
h Move left one character
l More right one character
kG Go to line k
. Repeat last edit (change, deletion) command
Deletion Commands
dd Delete current line
ndd Delete next n lines
nx Delete next n character
D Delete from current position to end of line