Notes on Cygwin

Notes on Cygwin

Download setup.exe from http://cygwin.com/, and install it.

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 define 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 define 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 file 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 prefix 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 file access mode.

ln -s d:/yuanyun/ws /ws

The .exe extension Executable program filenames 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 makefiles 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 makefiles. This problem can be solved by writing install and strip shell scripts to provide the extension ".exe" when needed.

The /proc filesystem

Cygwin supports the /proc virtual filesystem. The files in this directory are representations of various aspects of your system.

cat /proc/cpuinfo

One unique aspect of the Cygwin /proc filesystem is /proc/registry, which displays the Windows registry with each KEY as a directory and each VALUE as a file. 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 file pathname exists, the argument @pathname expands recursively to the content of pathname. Double quotes can be used inside the file 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 filenames to Cygwin POSIX-style pathnames and vice versa. It can be usedwhen a Cygwin programneeds to pass a file name to a native Windows program, or expects to get a file 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-specified signals such as SIGUSR1 to trigger actions within the program, like enabling debugging or re-opening log files. Each program defines 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 defines 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 prefix through the use of the "--change-cygdrive-prefix" option. We can set the automount prefix 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 filesystem 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 files.

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

  1. This library can be downloaded at www.STLPort.org.

  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 flags 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 figure 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 find 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 specific 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


Post a Comment

Labels

Java (159) Lucene-Solr (110) All (60) Interview (59) J2SE (53) Algorithm (37) Eclipse (35) Soft Skills (35) Code Example (31) Linux (26) JavaScript (23) Spring (22) Windows (22) Web Development (20) Tools (19) Nutch2 (18) Bugs (17) Debug (15) Defects (14) Text Mining (14) J2EE (13) Network (13) PowerShell (11) Chrome (9) Continuous Integration (9) How to (9) Learning code (9) Performance (9) UIMA (9) html (9) Design (8) Dynamic Languages (8) Http Client (8) Maven (8) Security (8) Trouble Shooting (8) bat (8) blogger (8) Big Data (7) Google (7) Guava (7) JSON (7) Problem Solving (7) ANT (6) Coding Skills (6) Database (6) Scala (6) Shell (6) css (6) Algorithm Series (5) Cache (5) IDE (5) Lesson Learned (5) Miscs (5) Programmer Skills (5) System Design (5) Tips (5) adsense (5) xml (5) AIX (4) Code Quality (4) GAE (4) Git (4) Good Programming Practices (4) Jackson (4) Memory Usage (4) OpenNLP (4) Project Managment (4) Python (4) Spark (4) Testing (4) ads (4) regular-expression (4) Android (3) Apache Spark (3) Become a Better You (3) Concurrency (3) Eclipse RCP (3) English (3) Firefox (3) Happy Hacking (3) IBM (3) J2SE Knowledge Series (3) JAX-RS (3) Jetty (3) Restful Web Service (3) Script (3) regex (3) seo (3) .Net (2) Android Studio (2) Apache (2) Apache Procrun (2) Architecture (2) Batch (2) Build (2) Building Scalable Web Sites (2) C# (2) C/C++ (2) CSV (2) Career (2) Cassandra (2) Distributed (2) Fiddler (2) Google Drive (2) Gson (2) Html Parser (2) Http (2) Image Tools (2) JQuery (2) Jersey (2) LDAP (2) Life (2) Logging (2) Software Issues (2) Storage (2) Text Search (2) xml parser (2) AOP (1) Application Design (1) AspectJ (1) Bit Operation (1) Chrome DevTools (1) Cloud (1) Codility (1) Data Mining (1) Data Structure (1) ExceptionUtils (1) Exif (1) Feature Request (1) FindBugs (1) Greasemonkey (1) HTML5 (1) Httpd (1) I18N (1) IBM Java Thread Dump Analyzer (1) JDK Source Code (1) JDK8 (1) JMX (1) Lazy Developer (1) Mac (1) Machine Learning (1) Mobile (1) My Plan for 2010 (1) Netbeans (1) Notes (1) Operating System (1) Perl (1) Problems (1) Product Architecture (1) Programming Life (1) Quality (1) Redhat (1) Redis (1) Review (1) RxJava (1) Solutions logs (1) Team Management (1) Thread Dump Analyzer (1) Troubleshooting (1) Visualization (1) boilerpipe (1) htm (1) ongoing (1) procrun (1) rss (1)

Popular Posts