打印

Cygwin使用指南

Cygwin使用指南

1 引言
cygwin是一个在windows平台上运行的unix模拟环境,是cygnus solutions公司开发的自由软件(该公司开发了很多好东西,著名的还有eCos,不过现已被Redhat收购)。它对于学习unix/linux操作环境,或者从unix到windows的应用程序移植,或者进行某些特殊的开发工作,尤其是使用gnu工具集在windows上进行嵌入式系统开发,非常有用。随着嵌入式系统开发在国内日渐流行,越来越多的开发者对cygwin产生了兴趣。本文将对其作一介绍。
2 机理
cygnus 当初首先把gcc,gdb,gas等开发工具进行了改进,使他们能够生成并解释win32的目标文件。然后,他们要把这些工具移植到windows平台上去。一种方案是基于win32 api对这些工具的源代码进行大幅修改,这样做显然需要大量工作。因此,他们采取了一种不同的方法——他们写了一个共享库(就是cygwin dll),把win32 api中没有的unix风格的调用(如fork,spawn,signals,select,sockets等)封装在里面,也就是说,他们基于 win32 api写了一个unix系统库的模拟层。这样,只要把这些工具的源代码和这个共享库连接到一起,就可以使用unix主机上的交叉编译器来生成可以在 windows平台上运行的工具集。以这些移植到windows平台上的开发工具为基础,cygnus又逐步把其他的工具(几乎不需要对源代码进行修改,只需要修改他们的配置脚本)软件移植到windows上来。这样,在windows平台上运行bash和开发工具、用户工具,感觉好像在unix上工作。


关于cygwin实现的更详细描述,请参考(楼下)

TOP

What Is Cygwin?Cygwin is a 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. The Cygwin DLL currently works with all recent, commercially released x86 32 bit and 64 bit versions of Windows, with the exception of Windows CE.
Note that the official support for Windows 95, Windows 98, and Windows Me will be discontinued with the next major version (1.7.0) of Cygwin.
What Isn't Cygwin?Cygwin is not a way to run native linux apps on Windows. You have to rebuild your application from source if you want it to run on Windows. Cygwin is not a way to magically make native Windows apps aware of UNIX ® functionality, like signals, ptys, etc. Again, you need to build your apps from source if you want to take advantage of Cygwin functionality.
Installing and Updating CygwinThe latest net releases of the Cygwin DLL are numbered 1.n.x, where "n" is currently "5" (e.g., 1.5.24). Any Cygwin program built from December 1998 onward should work correctly with 1.n.x DLLs.
The 1.n.x version numbering refers only to the Cygwin DLL. Individual packages like bash, gcc, less, etc. are released independently of the DLL. The setup.exe utility tracks the versions of all installed components and provides the mechanism for installing or updating everything available from this site for Cygwin. Run setup.exe any time you want to update or install a Cygwin package.
Note that, when installing packages for the first time, setup.exe does not install every package. Only the minimal base packages from the Cygwin distribution are installed by default. Clicking on categories and packages in the setup.exe package installation screen will provide you with the ability to control what is installed or updated. Clicking on the "Default" field next to the "All" category will provide you with the opportunity to install every Cygwin package. Be advised that this will download and install hundreds of megabytes to your computer. The best plan is probably to click on individual categories and install either entire categories or packages from the categories themselves.
Once you've installed your desired subset of the Cygwin distribution, setup.exe will remember what you selected so rerunning the program will update your system with any new package releases.


NewsNew Cygwin DLL 1.5.24-2 release
An update release for the Cygwin DLL and its accompanying utilities.
Jan 31 2007, Corinna Vinschen (Co-Project Leader)

New Cygwin DLL 1.5.24-1 release
An update release for the Cygwin DLL and its accompanying utilities.
Jan 26 2007, Corinna Vinschen (Co-Project Leader)

New Cygwin DLL 1.5.23-2 release
Dec 20 2006, Corinna Vinschen (Co-Project Leader)

New Cygwin DLL 1.5.23-1 release
Dec 19 2006, Corinna Vinschen (Co-Project Leader)

New Cygwin DLL 1.5.22-1 release
Nov 14 2006, Corinna Vinschen (Co-Project Leader)

New Cygwin DLL 1.5.21-1 release
Jul 23 2006, Christopher Faylor (Co-Project Leader)

New Cygwin DLL 1.5.20-1 release
Jul 2 2006, Christopher Faylor (Co-Project Leader)

New Cygwin DLL 1.5.19-4 release
Jan 20 2006, Christopher Faylor (Co-Project Leader)

New Cygwin DLL 1.5.19-3 release
Jan 19 2006, Christopher Faylor (Co-Project Leader)

New Cygwin DLL 1.5.19-1 release
Jan 16 2006, Christopher Faylor (Co-Project Leader)

more news
post new news
SoftwareSaVi 1.2.8
Jan 5 2006, [email=L.Wood@eim.surrey.ac.uk]Lloyd Wood[/email]

Entropy RSA P2P network
Jun 27 2005, Juergen Buchmueller

XView libraries
Mar 3 2005, Isaac Henry

SaVi satellite constellation visualization
Feb 16 2005, [email=L.Wood@eim.surrey.ac.uk]Lloyd Wood[/email]

Mortens Cygwin X-Launcher
Jul 20 2004, [email=martin.halle@gmx.net]Martin Halle[/email]

Starshiptraders Graphical Console (Client)
Aug 22 2003, David Johnson

Entropy P2P network
Jul 19 2003, Juergen Buchmueller

VREng (Virtual Reality Engine)
May 21 2003, Philippe Dax

TeXmacs
Dec 20 2002, Joris van der Hoeven

GrWinTk-0.99.9
Dec 9 2002, Tsuguhiro TAMARIBUCHI

more software
announce new software

Help, contact, web page, other info...For Cygwin licensing or commercial support, please visit the Red Hat Cygwin Product site. For all other questions and observations, please check the resources available at this site, such as the FAQ, the User's Guide and the mailing list archives. If you've exhausted these resources then please send email to an appropriate mailing list . This includes observations about web pages, setup questions, questions about where to find things, questions about why things are done a certain way, questions about the color preferences of Cygwin developers, questions about the meaning of the number 42, etc.
Please send notification of technical problems (bad html, broken links) concerning these web pages to the Cygwin mailing list.
Please do not send personal email with "quick questions" to individual Cygwin developers. The Cygwin mailing lists are the places for all questions. Really. I mean it.
Not responsible for errors in content, meaning, tact, or judgment. Live and let live. Toes go in first. I didn't do it. Enjoy. Cygwin DLL and utilities are Copyright © 2000, 2001,2002, 2003, 2004, 2005, 2006, 2007 Red Hat, Inc. Other packages have other copyrights.
UNIX ® is a registered trademark of the Open Group in the United States and other countries.

TOP

3 安装设置cygwin
3.1 安装
要安装网络版的cygwin,可以到http://cygwin.com,点击"Install Cygwin Now!"。这样会先下载一个叫做setup.exe的GUI安装程序,用它能下载一个完整的cygwin。按照每一屏的指示可以方便的进行安装。
3.2 环境变量
开始运行bash之前,应该设置一些环境变量。cygwin提供了一个.bat文件,里面已经设置好了最重要的环境变量。通过它来启动bash是最安全的办法。这个.bat文件安装在cygwin所在的根目录下。 可以随意编辑该文件。
CYGWIN变量用来针对cygwin运行时系统进行多种全局设置。开始时,可以不设置CYGWIN或者在执行bash前用类似下面的格式在dos框下把它设为tty
C:\> set CYGWIN=tty notitle glob
PATH 变量被cygwin应用程序作为搜索可知性文件的路径列表。当一个cygwin进程启动时,该变量被从windows格式(e.g. C:\WinNT\system32;C:\WinNT)转换成unix格式(e.g., /WinNT/system32:/WinNT)。如果想在不运行bash的时候也能够使用cygwin工具集,PATH起码应该包含x:\cygwin \bin,其中x:\cygwin 是你的系统中的cygwin目录。
HOME变量用来指定主目录,推荐在执行bash前定义该变量。当 cygwin进程启动时,该变量也被从windows格式转换成unix格式,例如,作者的机器上HOME的值为C:\(dos命令set HOME就可以看到他的值,set HOME=XXX可以进行设置),在bash中用echo $HOME看,其值为/cygdrive/c.
TERM变量指定终端型态。如果美对它进行设置,它将自动设为cygwin。
LD_LIBRARY_PATH被cygwin函数dlopen()作为搜索.dll文件的路径列表,该变量也被从windows格式转换成unix格式。多数Cygwin应用程序不使用dlopen,因而不需要该变量。
3.3 改变cygwin的最大存储容量
Cygwin 程序缺省可以分配的内存不超过384 MB(program+data)。多数情况下不需要修改这个限制。然而,如果需要更多实际或虚拟内存,应该修改注册表的 HKEY_LOCAL_MACHINE或HKEY_CURRENT_USER区段。田家一个DWORD键heap_chunk_in_mb并把它的值设为需要的内存限制,单位是十进制MB。也可以用cygwin中的regtool完成该设置。例子如下:
regtool -i set /HKLM/Software/Cygnus\ Solutions/Cygwin/heap_chunk_in_mb 1024
regtool -v list /HKLM/Software/Cygnus\ Solutions/Cygwin
4 使用cygwin
这一段讲一下cygwin和传统unix系统的不同之处。
4.1 映射路径名
4.1.1 引言
cygwin 同时支持win32和posix风格的路径,路径分隔符可以是正斜杠也可以是反斜杠。还支持UNC路径名。(在网络中,UNC是一种确定文件位置的方法,使用这种方法用户可以不关心存储设备的物理位置,方便了用户使用。在Windows操作系统,Novell Netware和其它操作系统中,都已经使用了这种规范以取代本地命名系统。在UNC中,我们不用关心文件在什么盘(或卷)上,不用关心这个盘(或卷)所在服务器在什么地方。我们只要以下面格式就可以访问文件:
\\服务器名\共享名\路径\文件名
共享名有时也被称为文件所在卷或存储设备的逻辑标识,但使用它的目的是让用户不必关心这些卷或存储设备所在的物理位置。)
符合posix标准的操作系统(如linux)没有盘符的概念。所有的绝对路径都以一个斜杠开始,而不是盘符(如c:)。所有的文件系统都是其中的子目录。例如,两个硬盘,其中之一为根,另一个可能是在/disk2路径下。
因为许多unix系统上的程序假定存在单一的posix文件系统结构,所以cygwin专门维护了一个针对win32文件系统的内部posix视图,使这些程序可以在windows下正确运行。在某些必要的情况下,cygwin会使用这种映射来进行win32和posix路径之间的转换。
4.1.2 cygwin mount表
cygwin 中的mount程序用来把win32盘符和网络共享路径映射到cygwin的内部posix目录树。这是与典型unix mount程序相似的概念。对于那些对unix不熟悉而具有windows背景的的人来说,mount程序和早期的dos命令join非常相似,就是把一个盘符作为其他路径的子目录。
路径映射信息存放在当前用户的cygwin mount表中,这个mount table 又在windows的注册表中。这样,当该用户下一次登录进来时,这些信息又从注册表中取出。mount 表分为两种,除了每个用户特定的表,还有系统范围的mount表,每个cygwin用户的安装表都继承自系统表。系统表只能由拥有合适权限的用户(windows nt的管理员)修改。
当前用户的mount表可以在注册表"HKEY_CURRENT_USER/Software/Red Hat, Inc./Cygwin/mounts v" 下看到。系统表
存在HKEY_LOCAL_MACHINE下。
posix 根路径/缺省指向系统分区,但是可以使用mount命令重新指向到windows文件系统中的任何路径。cygwin从win32路径生成posix路径时,总是使用mount表中最长的前缀。例如如果c:被同时安装在/c和/,cygwin将把C:/foo/bar转换成/c/foo/bar.
如果不加任何参数地调用mount命令,会把Cygwin当前安装点集合全部列出。在下面的例子中,c盘是POSIX根,而d盘被映射到/d。本例中,根是一个系统范围的安装点,它对所有用户都是可见的,而/d仅对当前用户可见。
c:\> mount
f:\cygwin\bin on /usr/bin type system (binmode)
f:\cygwin\lib on /usr/lib type system (binmode)
f:\cygwin on / type system (binmode)
e:\src on /usr/src type system (binmode)
c: on /cygdrive/c type user (binmode,noumount)
e: on /cygdrive/e type user (binmode,noumount)
还可以使用mount命令增加新的安装点,用umount删除安装点。
当Cygwin 不能根据已有的安装点把某个win32路径转化为posix路径时,cygwin会自动把它转化到一个处于缺省posix路径/cygdrive下的的一个安装点. 例如,如果Cygwin 访问Z:\foo,而Z盘当前不在安装表内,那么Z:\将被自动转化成/cygdrive/Z.
可以给每个安装点赋予特殊的属性。自动安装的分区显示为“auto”安装。安装点还可以选择是"textmode"还是 "binmode",这个属性决定了文本文件和二进制文件是否按同样的方式处理。
4.1.3 其他路径相关信息
cygpath工具提供了在shell脚本中进行win32-posix路径格式转换的能力。
HOME, PATH,和LD_LIBRARY_PATH环境变量会在cygwin进程启动时自动被从Win32格式转换成了POSIX格式(例如,如果存在从该win32路径到posix路径的安装,会把c:\cygwin\bin转为/bin)。

TOP

学习一下,以后可能回用到。

TOP


感谢一直以来您对我们的支持!
当前时区 GMT+8, 现在时间是 2008-9-7 12:29 京ICP证060528 号

Designed By 17DST