### Notes on Cygwin

Open “System Variables “, add the path to Cygwin's bin directory to the end of “Path” variable, then we can use unix commands such as ls, ps, in windows command-prompt.

Set up Environment Variables

The CYGWIN variable is used to configure many global settings for the Cygwin runtime system.

The PATH environment variable is used by Cygwin applications as a list of directories to search for executable files to run.

The HOME environment variable is used by many programs to determine the location of your home directory

The TERM environment variable specifies your terminal type. It is automatically set to cygwin if you have not set it to something else.

The LD_LIBRARY_PATH environment variable is used by the Cygwin function dlopen () as a list of directories to search for .dll files to load.

Customizing bash

In home directory, you can see the following files: .bash_profile(or .profile),.bashrc, .inputrc

.bash_profile is executed when bash is started as login shell, e.g. from the command bash --login. This is a useful place to define and export environment variables and bash functions that will be used by bash and the programs invoked by bash. It is a good place to redefine PATH if needed.

such as adding a ":." to the end of PATH to also search the current working directory,

PATH=$PATH:. unsetting MAILCHECK or deﬁne MAILPATH to point to your existing mail inbox in order to avoid delays you should. .bashrc is similar to .profile but is executed each time an interactive bash shell is launched. It serves to deﬁne elements such as aliases. bashrc is not called automatically for login shells. You need source it from .bash_profile. .inputrc controls how programs using the readline library (including bash) behave. It is loaded automatically. Consider the following settings: # Ignore case while completing set completion-ignore-case on # Make Bash 8bit clean set meta-flag on set convert-meta off set output-meta on Tools that do not use readline for display, such as less and ls, require additional settings, which could be put in your .bashrc: alias less=’/bin/less -r’ alias ls=’/bin/ls -F --color=tty --show-control-chars’ Using Cygwin Mapping path names Cygwin supports both Win32- and POSIX-style paths, where directory delimiters may be either forward or back slashes. UNC pathnames (starting with two slashes and a network name) are also supported. Cygwin maintains a special internal POSIX view of the Win32 ﬁle system that allows these programs to successfully run under Windows. Cygwin uses this mapping to translate between Win32 and POSIX paths as necessary. The Cygwin Mount Table The mount utility program is used to to map Win32 drives and network shares into Cygwin’s internal POSIX directory tree.Whenever Cygwin generates a POSIX path from a Win32 one, it uses the longest matching preﬁx in the mount table. Invoking mount without any arguments displays Cygwin’s current set of mount points. Whenever Cygwin cannot use any of the existing mounts to convert from a particular Win32 path to a POSIX one, Cygwin will automatically default to an imaginary mount point under the default POSIX path /cygdrive. Z:\ would be automatically converted to /cygdrive/Z. mount; mount c: /c Additional Path-related Information Symbolic links can also be used to map Win32 pathnames to POSIX, but symbolic links cannot set the default ﬁle access mode. ln -s d:/yuanyun/ws /ws The .exe extension Executable program ﬁlenames end with .exe but the .exe need not be included in the command, so that traditional UNIX names can be used. but, for programs that end in .bat and .com, you cannot omit the extension. If a shell script myprog and a programmyprog.exe coexist in a directory, the program has precedence and is selected for execution of myprog. The gcc compiler produces an executable named filename.exe when asked to produce filename. This allows many makeﬁles written for UNIX systems to work well under Cygwin. Unfortunately, the install and strip commands do distinguish between filename and filename.exe. They fail when working on a non-existing filename even if filename.exe exists, thus breaking some makeﬁles. This problem can be solved by writing install and strip shell scripts to provide the extension ".exe" when needed. The /proc ﬁlesystem Cygwin supports the /proc virtual ﬁlesystem. The ﬁles in this directory are representations of various aspects of your system. cat /proc/cpuinfo One unique aspect of the Cygwin /proc ﬁlesystem is /proc/registry, which displays the Windows registry with each KEY as a directory and each VALUE as a ﬁle. As anytime you deal with the Windows registry, use caution since changes may result in an unstable or broken system. cd /proc/registry;ls The @pathnames To circumvent the limitations on shell line length in the native Windows command shells, Cygwin programs expand their arguments starting with "@" in a special way. If a ﬁle pathname exists, the argument @pathname expands recursively to the content of pathname. Double quotes can be used inside the ﬁle to delimit strings containing blank space. Embedded double quotes must be repeated. Cygwin Utilities cygcheck cygpath The cygpath program is a utility that converts Windows native ﬁlenames to Cygwin POSIX-style pathnames and vice versa. It can be usedwhen a Cygwin programneeds to pass a ﬁle name to a native Windows program, or expects to get a ﬁle name from a nativeWindows program. dumper The dumper utility can be used to create a core dump of running Windows process. This core dump can be later loaded to gdb and analyzed. One common way to use dumper is to plug it into cygwin’s Just-In-Time debugging facility by adding error_start=x:\path\to\dumper.exe to the CYGWIN environment variable. If error_start is set this way, then dumper will be started whenever some program encounters a fatal error. dumper can be also be started from the command line to create a core dump of any running process. Unfortunately, because of a Windows API limitation, when a core dump is created and dumper exits, the target process is terminated too. kill -f, --force force, using win32 interface if necessary The kill program allows you to send arbitrary signals to other Cygwin programs. The usual purpose is to end a running program from some other window when ^C won’t work, but you can also send program-speciﬁed signals such as SIGUSR1 to trigger actions within the program, like enabling debugging or re-opening log ﬁles. Each program deﬁnes the signals they understand. You may need to specify the full path to use kill from within some shells, including bash, the default Cygwin shell. This is because bash deﬁnes a kill builtin function; To make sure you are using the Cygwin version, use: /bin/kill --version mount Cygdrive mount points Whenever Cygwin cannot use any of the existingmounts to convert froma particular Win32 path to a POSIX one, Cygwin will, instead, convert to a POSIX path using a default mount point: /cygdrive. The mount utility can be used to change this default automount preﬁx through the use of the "--change-cygdrive-preﬁx" option. We can set the automount preﬁx to /: mount --change-cygdrive-prefix / ps -a, --all show processes of all users -e, --everyone show processes of all users -W, --windows show windows as well as cygwin processes regtool: View or edit the Win32 registry umount -A, --remove-all-mounts remove all mounts -c, --remove-cygdrive-prefix remove cygdrive prefix -s, --system remove system mount (default) -S, --remove-system-mounts remove all system mounts -u, --user remove user mount -U, --remove-user-mounts remove all user mounts Using Cygwin effectively with Windows Many Windows utilities provide a good way to interact with Cygwin’s predominately command-line environment In cygwin, you can also call windows commands, such as ipconfig, net.exe, notepad.exe. Most of these tools support the /? switch to display usage information. Pathnames Windows programs do not understand POSIX pathnames, so any arguments that reference the ﬁlesystem must be in Windows (or DOS) format or translated. Cygwin provides the cygpath utility for converting between Windows and POSIX paths. notepad.exe "$(cygpath -aw "Desktop/Phone Numbers.txt")"

A few programs require a Windows-style, semicolon-delimited path list, which cygpath can translate from a POSIX path with the -p option.

javac -cp "$(cygpath -pw "$CLASSPATH")" hello.java

The cygutils package

Unix tools such as tr can convert between CRLF and LF endings, but cygutils provides several dedicated programs: conv, d2u, dos2unix, u2d, and unix2dos.

Creating shortcuts with cygutils

The cygutils package includes a mkshortcut utility for creating standard Microsoft .lnk ﬁles.

Printing with cygutils

There are several options for printing from Cygwin, including the lpr found in cygutils.

The easiest way to use cygutils’ lpr is to specify a default device name in the PRINTER environment variable. You may also specify a device on the command line with the -d or -P

options, which will override the environment variable setting.

A device name may be a UNC path (\\server_name\printer_name), a reserved DOS device name (prn, lpt1), or a local port name that is mapped to a printer share.

lpr sends raw data to the printer; no formatting is done. Many, but not all, printers accept plain text as input. If your printer supports PostScript, packages such as a2ps and enscript can prepare text ﬁles for printing. The ghostscript package also provides some translation from PostScript to various native printer languages. Additionally, a native Windows application for printing PostScript, gsprint, is available from the Ghostscript website.

Programming with Cygwin

Using GCC with Cygwin

Console Mode Applications

gcc –help,gcc hello.c -o hello.exe,g++ Welcome.cpp -o Welcome.exe

The g++ command signifies that the C++ complier should be used instead of the C compiler.

Compiling Programs with Multiple Source Files

Compiling a program, which has two or more source files, can be accomplished two ways. The first method requires listing all the files on the command line. The second method takes advantage of Cygwin’s wild-card character(*).

g++ *.cpp -o Fig06_05

The STLPort Library

2. The library must be installed from the Cygwin prompt.

3. Enter the command make -f gcc-cygwin.mak to start the creation of the install files

4. When completed, enter make -f gcc-cygwin.mak install to begin the installation of the new library The new library should now be installed and working. To test it, compile a program using g++ -I /usr/Local/Include/STLPort File.cpp -L / usr/Local/lib -lSTLPort_Cygwin -o ExecutableFile.

Using the GDB Debugger

Before you can debug your program, you need to prepare your program for debugging. What you need to do is add -g to all the other ﬂags you use when compiling your sources to objects.

gcc -g myapp.c -o myapp, g++ Debug.cpp -o Debug -g

What this does is add extra information to the objects (they get much bigger too) that tell the debugger about line numbers, variable names, and other useful things. These extra symbols and debugging information give your program enough information about the original sources so that the debugger can make debugging much easier for you.

To invoke GDB, simply type gdb Debug.exe, then (gdb) will appear to prompt you to enter commands, like run or help.

If your program crashes and you’re trying to ﬁgure out why it crashed, the best thing to do is type run and let your program run. After it crashes, you can type where to ﬁnd out where it crashed, or info locals to see the values of all the local variables. There’s also a print that lets you look at individual variables or what pointers point to.

If your program is doing something unexpected, you can use the break command to tell gdb to stop your program when it gets to a speciﬁc function or line number:

break 47, break my_function

when you type run your programwill stop at that "breakpoint" and you can use the other gdb commands to look at the state of your program at that point, modify variables, and step through your program’s statements one at a time.

You may specify additional arguments to the run command to provide command-line arguments to your program.

Debugging with command line arguments

myprog -t foo --queue 47

gdb myprog

(gdb) run -t foo --queue 47

Use Cygwin in Eclipse.org CDT

Use Linux's telnet on cygwin

In order to use Linux's telnet on cygwin instead of windows version, we just need to install inetutils package (in category Net) .

Some Little Trick:

Problem:

When I connect to some Lunix/Unix machines, and use vi, it reports 'Unknown terminal: cygwin'.

This is because the "cygwin" terminal type is cygwin's emulation of a UNIX terminal in a windows "dos box". Since it's not a "standard" terminal type, it's not included in a lot of terminfo databases.

Solution:

Add 'export TERM=xterm' to .bashrc or .kshrc etc.

Trick2: Use backslash(\) directly as path delimiter on cygwin

On cygwin, we can not directly call 'cd D:\dira\dirb' to change directory, we can type 'cd D:/dira/dirb' or 'cd D:\\dira\\dirb'.But this is somewhat inconvenient.

We can use one little trick, add sinle quote to the path.

cd 'D:\dira\dirb'

So now, we can just copy the dir path from windows explore, and paste in cygwin's command line,

Trick3: Display Chinese in Cygwin

Set home directory if have not done it, and change to home directory.

1. edit ~/.inputrc, and add the following lines:

set meta-flag on

set input-meta on

set convert-meta off

set output-meta on

2. edit ~/.bash_profile, and add the following line

alias ls='ls –show-control-chars'

Resources

Cygwin User's Guide

DiveIntoCygwinGCC.pdf

http://ras52-tech.blogspot.com/2007/01/telnet-on-cygwin.html

http://www.nabble.com/Could-I-use-backslash-directly-as-path-delimiter-on-cygwin--to21273102.html#a21273528

http://pinglunliao.blogspot.com/2006/05/chinese-in-cygwin.html