Red Hat Docs  >  Manuals  >  GNUPro Toolkit Manuals  > 

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7. Using Cygwin

This section discusses the architecture underlying the Cygwin tools for porting UNIX applications to Windows 32-bit environments. For more information, see:

 
http://sources.redhat.com/cygwin/

GNUPro Toolkit provides the following packages for Cygwin:

GNUPro development tools:
binutils, bison, byacc, dejagnu, diff, expect, flex, as, gcc, gdb, itcl, ld, libstdc++, make, patch, tcl, tix, tk.

GNUPro contributed tools:
ash, bash, bzip2, diff, fileutils, findutils, gawk, grep, gzip, m4, sed, shellutils, tar, textutils, ssh, cvs, rsync, perl, ssh, python, ssl, vim, time, and more....

Cygwin emulates a standard UNIX directory structure. To ensure the structure is the same, always have /tmp both with and without the mount table translations. To emulate the /etc directory (so that the ls -l UNIX directory contents listing command works), use the following example's declarations as a guide:

 
$ mkdir /etc/
$ cd /etc
$ mkpasswd > /etc/passwd
$ mkgroup > /etc/group

Cygwin comes with both bash.exe and sh.exe shells. sh.exe is based on ash. In case of trouble with ash, make sh.exe point to the bash.exe shell.

When executing a binary linked against the library, the Cygwin dynamically linked library (DLL) loads into the application's text segment. To emulate a UNIX kernel for allowing access to all processes that can run with it, use the Cygwin DLL. The Cygwin DLL will create shared memory areas so that other processes using separate instances of the DLL can access the kernel. The DLL keeps track of open file descriptors and, among other purposes, assists fork and exec calls. In addition to the shared memory regions, every process also has a per-process structure that contains information such as process ID, user ID, signal masks, and other similar process-specific information.

The DLL uses the Win32 API. Because processes run under the standard Win32 subsystem, they can access both the UNIX compatibility calls provided by Cygwin as well as any of the Win32 API calls. This gives the programmer complete flexibility in designing the structure of their program in terms of the API in use; for example, a project might require a Win32-specific GUI using Win32 API calls on top of a UNIX back-end that uses Cygwin.

The CYGWIN environment variable helps to override the Win32 default behavior and force POSIX(9) standards compliance.

UNIX applications that have to switch the user context have the setuid and seteuid calls, which are not part of the Microsoft Windows API. Nevertheless these calls are supported under Microsoft Windows NT, Microsoft Windows 2000 and Microsoft Windows XP with Cygwin.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.1 Before Using Cygwin

Before using Cygwin tools, see descriptions of See Mount Table and the mount Utility, See section Text and Binary Modes with Cygwin, See section File Permissions With Cygwin, See section Special Cygwin File Names, See section Building and Using DLLs with Cygwin, and See section Defining Windows Resources for Cygwin.

Mount Table and the mount Utility

The mount utility controls a mount table for emulating a POSIX view of a Windows file system space. To change a Windows path that uses / and mount arbitrary Win32 paths into the POSIX file system space, use mount to mount each drive letter under a slash partition (such as C:\ to /c or D:\ to /d, and so forth).

Executing the mount command without any arguments prints the current mount table to a screen. Alternatively, provide the intended Win32 path to mount as a first argument and the POSIX path as the second argument.

In executing the mount command without any arguements, you may note that there are already some mount points created. By default, the graphical installer will update the mount table to use the Contributed Binaries as / and make /opt/redhat resolve like it would on a Unix system. If an old mount table was already on the system, it can be restored by running the restoremounts.bat file in the installation directory.

If desired, one may issue the following command to remove all mount points.

 
$ mount --remove-all-mounts

The example example Using mount, demonstrates mounting C:/cygwin/H-i686-pc-cygwin/bin to /bin (assuming /bin exists), as well as mounting \\pollux\home\joe\data to a /data network directory, making /bin/sh a valid shell, to satisfy make.

IMPORTANT
{With a UNIX derived shell like bash or tcsh, the backward slash
will not work; substitute / or use surround the path with single quotes.}

Example Using mount

 
$ ls /bin /data
ls: /data: No such file or directory
$ mount C:\cygwin\H-i686-pc-cygwin\bin /bin
$ mount \\pollux\home\joe\data /data
Warning: /data does not exist!
$ mount
Device Directory Type Flags
\\pollux\home\joe\data /data native text!=binary
C:\cygwin\H-i686-pc-cygwin\bin /bin native text!=binary
D: /d native text!=binary
C: / native text!=binary
$ ls /bin/sh
/bin/sh

Text and Binary Modes with Cygwin

The following documentation discusses some of the main distinction with text and binary modes with UNIX and Microsoft Windows interoperability, and how Cygwin solves the problems. See section File Permissions With Cygwin, and See section Special Cygwin File Names. On a UNIX system, when an application reads from a file it gets exactly what's in the file on disk and the same is true for writing to the file. The situation is different in the DOS and Microsoft Windows world where a file can be opened in one of two modes, either binary or text. In the binary mode, the system behaves exactly as in UNIX. However in text mode there are major differences:

The mode can be specified explicitly. In an ideal DOS and Microsoft Windows world, all programs using lines as records (such as bash, make, or sed) would open files (changing the mode of their standard input and output) as text. All other programs (such as cat, cmp, or tr) would use binary mode. In practice with Cygwin, programs that deal explicitly with object files specify binary mode (as is the case of od, which is helpful to diagnose CR problems). Most other programs (such as cat, cmp, tr) use the default mode. The Cygwin system gives us some flexibility in deciding how files are to open when the mode is not specified explicitly:

UNIX programs that have been written for maximum portability will know the difference between text and binary files and act appropriately under Cygwin. For those programs, the text mode default is a good choice. Programs included in official distributions should work well in the default mode.

Text mode makes it much easier to mix files between Cygwin and Microsoft Windows programs, since Microsoft Windows programs will usually use the carriage return/line feed (CR/LF) format. Unfortunately you may still have some problems with text mode. First, some of the utilities included with Cygwin do not yet specify binary mode when they should. Second, you will introduce CRs in text files you write, causing problems when moving them back to a UNIX system.

If you are mounting a remote file system from a UNIX machine, or moving files back and forth to a UNIX machine, you can access them in binary mode since text files found there will normally be NL format anyway, and you would want any files put there by Cygwin programs to be stored in a format that the UNIX machine will understand. Remove CRs from all Makefiles and shell scripts and make sure that you only edit the files with DOS/Windows editors that can cope with binary mode files.

IMPORTANT
{You can decide this on a disk by disk basis (for example,
mounting local disks in text mode and network disks in binary mode). You can also partition a disk, for example by mounting c: in text mode, and c:\home in binary mode.}

File Permissions With Cygwin

On Windows 95 and Windows 98 systems, files are always readable, and Cygwin uses the native read-only mode to determine if they are writable. Files are executable if the filename ends with .bat or .com or .exe file extensions, or if #! starts. Consequently, chmod can only affect the w mode,whereas it silently ignores actions involving the other modes. Under NT, file permissions default to the same behavior as Windows 95 and Windows 98 systems. However, there is optional functionality in Cygwin that can make file systems behave more like files for UNIX systems; for instance, use the ntea option to the CYGWIN environment variable (see Environment Variables for Cygwin). When using ntea, Cygwin will start with basic permissions, storing POSIX file permissions in NT Extended Attributes. On NTFS partitions, the attributes can be stored sensibly inside the normal NTFS filesystem structure. However, on a FAT partition, NT stores extended attributes in a flat file at the root of the EA DATA. SF. file partition, which can grow to extremely large sizes if you have a large number of files on the partition, slowing the system processes. In addition, the EA DATA. SF. file can only be deleted outside of Windows because of its in use status. For these reasons, the use of NT Extended Attributes is off by default in Cygwin. Specifying ntea in CYGWIN has no effect for Windows 95 and Windows 98 systems. Under NT, the [ -w filename ] test is only true if filename is writable across the board, such as with a chmod +w filename call. However, ntea only simulates the permissions inside of Cygwin. On NT systems (NT, 2000, XP) you can use real file permissions when using the NTFS filesystem. In this case it might makes sense to set ntsec in the CYGWIN environment variable. Then Cygwin uses the NT file permission access control lists to set POSIX like permissions.

IMPORTANT
{In contrast to FAT, FAT32 does not support storing extended
attributes.}

Special Cygwin File Names

The following documentation discusses some special file naming usage by Cygwin.

Defining Windows Resources for Cygwin

windres reads a Windows resource file (*.rc) and converts it to a .res COFF file. The syntax and semantics of the input file are the same as for any other resource compiler; see any publication describing the Windows resource format for details.

windres compiles a .res file to include all the bitmaps, icons, and other resources you need, into one object file. Omitting the -O coff declaration would create a Windows .res format file without linkable COFF objects. Instead, windres produces a COFF object, for compatibility with how a linker can handle Windows resource files directly, maintaining the.res naming convention.

For more information on windres, see Using binutils in GNUPro Auxiliary Development Tools.

Building and Using DLLs with Cygwin

The following documentation provides an example of how to build a .dll file, using a single file, myprog .c, for the program, myprog .exe, and a single file, mydll .c, for the contents of the .dll file, mydll .dll, then compiling everything as objects.

 
gcc -shared myprog.c -o mydll .dll -e _mydll_init@12

Now, when you build your program, you link against the import library, with declaration's like the following example's commands.

 
gcc myprog.o mydll.dll -o myprog.exe


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.1.1 Using GCC with Cygwin

The following documentation discusses using the GNUPro compiler with Cygwin to compile like with UNIX. Use the following command in a shell console.

 
$ gcc hello.c -o hello.exe
$ hello.exe
Hello, World

Cygwin allows you to build programs with full access to the standard Windows 32-bit API, including the GUI functions (as defined in Microsoft publications); however, the process of building those applications is slightly different using the GNU tools instead of the Microsoft tools. Your sources will not need to change; just remove all __export attributes from functions and replace them, as the following example shows.

 
int foo (int) __attribute__((__dllexport__));

int
foo (int i)
{
  ...

For most cases, remove the __export attributes.

The Makefile is similar to any other UNIX-like or Cygwin Makefile. The only difference is that you use a gcc -mwindows declaration to link your program (myapp.exe in the following example's script) into a GUI application instead of into a command line application.

 
myapp.exe : myapp.o myapp.res
        gcc -mwindows myapp.o myapp.res -o $@
myapp.res : myapp.rc resource.h
        windres $< -O coff -o $@


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.1.2 Debugging Cygwin Programs

Before you can debug your program, you need to prepare your program for debugging. Add a -g declaration to all the other flags you use when compiling your sources to objects, in order to add extra information to the objects (making them much bigger), and to provide critical information to the debugger regarding line numbers, variable names, and other useful things; these extra symbols and debugging data give your program enough information about the original sources so that the debugger can resolve the problems. Use declarations like the following example's commands.

 
gcc -g -O2 -c myapp.c
gcc -g myapp.c -o myapp

To invoke gdb, use the gdb myapp.exe declaration (substituting the executable file's name for myapp). The copyright text displays followed by the (gdb) prompt, waiting for you to enter commands like run or help.

If your program crashes and you re trying to determine why it crashed, the best thing to do is type run and let your program run. After it crashes, you can use the where command to determine where it crashed, or the info locals call to see the values of all the local variables. The print declaration lets you examine individual variables or a line to which pointers point. If your program is doing something unexpected, use the break command to tell the debugger to stop your program when the debugging process gets to a specific function or line number.

Using the run command, stop your program at a breakpoint, and use other gdb commands to look at the state of your program at that point, to modify variables, and to step through your program's statements one at a time.

Use the help command to get a list of all the commands to use, or see Debugging with GDB in GNUPro Debugging Tools.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

7.1.3 Environment Variables for Cygwin

Before starting bash, you must set some environment variables, some of which can also be set or modified inside bash. You have a .bat file where the most important ones are set before initially invoking bash. The fully editable.bat file installs by default in \..\cygwin\cygnus.bat and the Start menu points to it.

The most important environment variable is the CYGWIN variable. The CYGWIN variable is used to configure many global settings for the Cygwin runtime system. Initially you can leave CYGWIN unset or set it to tty using input like the following example's syntax in a DOS shell, before launching bash.

 
C:\..\> set CYGWIN=tty notitle

The PATH environment variable is used by Cygwin applications as a list of directories to search for executable files to run. Convert this environment variable, when a Cygwin process first starts, from a Microsoft Windows format (C:\WinNT\system32;C:\WinNT) to UNIX format (/WinNT/system32:/WinNT).

Set the PATH environment variable so that, before launching bash, it contains at least a bin directory: C:\..\cygwin\H-i686-pc-cygwin\bin.

make uses an environment variable, MAKE_MODE lines. If you get errors from make with the /c not found message, set MAKE_MODE to UNIX with the following example's form.
 
C:\> set MAKE_MODE=UNIX

The HOME environment variable is used by UNIX shells to determine the location of your home directory. This environment variable is converted from the Microsoft Windows format (that is, C:\home\bob) to UNIX format (that is, /home/bob) when a Cygwin process first starts. To prevent confusion, ensure that HOME and /etc/passwd agree on your home directory.

The TERM environment variable specifies your terminal type. It is set it to cygwin by default.

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. This environment variable is converted from the Microsoft Windows format (that is, C:\WinNT\system32;C:\WinNT) to UNIX format (that is, /WinNT/system32:/WinNT) when a Cygwin process first starts.

The CYGWIN environment variable is used to configure many global settings for the Cygwin runtime system, using the following options.

IMPORTANT
{Each option is separated by others with a space. Many
options can be turned off by prefixing with no (such as nobar or bar options).}

(no)binmode
If set, unspecified file opens by default to binary mode (no CR/LF translations) instead of text mode. This option must be set before starting a Cygwin shell to have an effect on redirection. On by default.

(no)envcache
If set, environment variable conversions (between Win32 and POSIX) are cached. Note that this is may cause problems if the mount table changes, as the cache is not invalidated and may contain values that depend on the previous mount table contents. Defaults to set.

(no)export
If set, the final values of these settings are re-exported to the environment as $CYGWIN again.

(no)title
If set, the title bar reflects the currently running program's name. Off by default.

(no)glob
If set, command line arguments containing UNIX-style file wildcard characters (brackets, question mark, asterisk) are expanded into lists of files that match those wildcards. This is applicable only to programs running from a windows command line prompt. Set by default.

(no)tty
If set, Cygwin enables extra support (such as termios) for UNIX-like tty calls. Off by default.

(no)ntea
If set, use the full Microsoft Windows NT Extended Attributes to store UNIX-like inode information.

WARNING!
ntea
partitions. ntea only operates under Microsoft Windows NT. Off by default.

(no)ntsec
If ntsec is set, use the Microsoft Windows NT security model to set UNIX-like permissions on files and processes. The file permissions can only be set on NTFS partitions. FAT and FAT32 don t support the Microsoft Windows NT file security (for more information, see http://sources.redhat.com/cygwin/). Defaults to be not set.

(no)smbntsec
If smbntsec is set, use ntsec on remote drives as well (this is the default). If you encounter problems with Microsoft Windows NT shares or Samba drives, setting this to nosmbntsec could help. In that case the permission and owner/group information is faked as on FAT partitions. A reason for a non working ntsec on remote drives could be insufficient permissions of the users. Since the needed user rights are somewhat dangerous, it's not always an option to grant those rights to users. However, this shouldn t be a problem in Microsoft Windows NT domain environments. Default is smbntsec.

(no)reset_com
If reset_com is set, serial ports are reset to 9600-8-N-1 with no flow control when used. This is done at open time and when handles are inherited. Default is reset_com.

(no)strip_title
If strip_title is set, strips directory part off the window title, if any. Defaults to strip_title.

(no)title
If title is set, the title bar reflects the name of the program currently running. Under Microsoft Windows 95 and Microsoft Windows 98, the title bar is always enabled and it is stripped by default, but this is because of the way Microsoft Windows 95 and Microsoft Windows 98 work. In order not to strip, specify title or title nostrip_title. Defaults to nostrip_title.

Special Options with the CYGWIN Environment Variable

The CYGWIN environment variable uses the following options. Each option is separated by others with a space. To turn off any option, prefix it with no (such as the nobinmode for the binmode option).

binmode
Opens an unspecified file by default in binary mode, with no CR /LF translations, instead of text mode. binmode must be set before starting a Cygwin shell to have an effect on redirection.

envcache
Caches environment variable conversions (between Win32 and POSIX); this may cause problems if the mount table changes, as the cache is not invalidated and may contain values that depend on the previous mount table contents.

export
Re-exports the final values of these settings to the environment as $CYGWIN again.

title
Makes the title bar provide the currently running program's name.

strip_title
Strips the directory part off the window title.

glob
Expands command line arguments containing UNIX-style file wildcard characters (brackets, question mark, asterisk) into lists of files that match those wildcards. Applicable only to programs running from a Windows command line prompt.

tty
Enables extra support (such as termios) for UNIX-like tty calls. Off by default.

strace=n[:cache][,filename]
Configures system tracing. Off by default, setting various bits in n (a bit flag) enables various types of system messages. Setting n to 1 enables most messages. Other values can be found in the sys/strace.h directory. : cache lets you specify how many lines to cache before flushing the output; for example, type strace=1:20 to cache 20 lines. filename lets you send messages to a file instead of to the shell's buffer.

ntea
Allows use of full NT Extended Attributes to store UNIX-like inode information.

WARNING!
The ntea
create additional large files on non-NTFS partitions.

reset_com
If set, serial ports are reset to 9600-8-N-1 with no flow control when used. This is done at open time and when handles are inherited.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Brendan Conoboy on June, 5 2003 using texi2html