| Red Hat Docs > Manuals > GNUPro Toolkit Manuals > |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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:
binutils, bison, byacc, dejagnu,
diff, expect, flex, as,
gcc, gdb, itcl, ld, libstdc++, make, patch, tcl, tix, tk.
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] | [ ? ] |
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 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.
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 |
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:
\n, ^J), is
transformed into a carriage return/new line sequence, or CR (\r,
^M) NL.
^Z character signals the end of file.
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:
mount), then
the default is specified by the mount flag.
The CYGWIN environment variable will affect a disk file when you
are using stdio redirection from the Microsoft Windows
prompt. Otherwise, the mode of the file is based on the mode specified
by a --change-cygdrive-prefix call, as the following example
shows (which makes the default mode be binary).
mount -b --change-cygdrive-prefix /cygdrive |
If the file is a symbolic link, the mode of the target file system applies.
When redirecting, the Cygwin shells uses the first three rules. For
these shells, the relevant value of CYGWIN is that at the time
the shell was launched and not that at the time the program is executed.
Non-Cygwin shells always pipe and redirect with binary mode. With
non-Cygwin shells, the cat filename | program
and program < filename commands are not equivalent
when filename is on a text-mounted partition. To illustrate the
various rules, the following example's script deletes CRs from files by
using the tr program, which can only write to standard output.
#!/bin/sh
# Remove \r from the files given as arguments
for file in "$@"
do
CYGWIN=binmode sh -c "tr -d \\\"\\\r\\\" < '$file' > c:tmpfile.tmp"
if [ "$?" = "0" ]
then
rm "$file"
mv c:tmpfile.tmp "$file"
fi
done
|
The script works irrespective of the mount because the second rule
applies for the path, c:tmpfile.tmp. According to the fourth
rule, CYGWIN must be set before invoking the shell. These
precautions are necessary because tr does not set its standard
output to binary mode. It would thus reintroduce \r when writing
to a file on a text mounted partition. The desired behavior can also be
obtained by using tr -d \r in a .bat file.
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.
c: in text
mode, and c:\home in binary mode.}
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.
The following documentation discusses some special file naming usage by Cygwin.
AUX,
COM1, LPT1 or PRN cannot be used in a regular
Cygwin Windows or POSIX path, even with an extension
(prn.txt). However, the special names can be used as filename
extensions (file.aux). You can use the special names as you would
under DOS; for example, you can print on your default printer with the
cat filename > PRN command (making sure to end with
a Form Feed).
/dev directory as it is
automatically simulated within Cygwin. It supports the following
devices: /dev/null, /dev/tty and/dev/comX (the
serial ports). These devices cannot be seen with the ls /dev
command, although commands such as ls /dev/tty work fine.
.exe extension for executable program filenames end with
.exe although the .exe extension is not necessary in a
command, so that traditional UNIX names can be used. On the contrary,
you cannot omit .bat and .com extensions. As a side
effect, the ls filename declaration gives information about
filename .exe if filename .exe exists and
filename does not. In the same situation, call stat("
filename " ...) to give information about the
filename .exe file.
Differentiate any two files by examining their inodes.
.exe executable when
asked to produce filename, allowing many makefiles written for
UNIX systems to work well under Cygwin. Unfortunately the install
and strip commands do distinguish between filename and a
filename .exe file. They fail when working on a
non-existing filename even if filename .exe exists,
thus breaking some makefiles. To solve this problem, write
install and strip shell scripts to provide the .exe
extension.
@pathname circumvents the limitations on shell line length in
the native Windows command shells, allowing Cygwin programs to expand
their arguments by starting with @ in a special way. If a file
pathname exists, the @pathname argument expands recursively to
the content of the program's pathname. Use double quotes in the file's
content to delimit strings containing blank space.
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.
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 |
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] | [ ? ] |
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] | [ ? ] |
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] | [ ? ] |
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.
|
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.
no (such as
nobar or bar options).}
(no)binmode
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
(no)export
$CYGWIN again.
(no)title
(no)glob
(no)tty
termios) for
UNIX-like tty calls. Off by default.
(no)ntea
ntea
ntea only operates under Microsoft Windows NT. Off
by default.
(no)ntsec
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
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
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
strip_title is set, strips directory part off the window
title, if any. Defaults to strip_title.
(no)title
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.
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
CR
/LF translations, instead of text mode. binmode must be
set before starting a Cygwin shell to have an effect on redirection.
envcache
export
$CYGWIN again.
title
strip_title
glob
tty
termios) for UNIX-like tty
calls. Off by default.
strace=n[:cache][,filename]
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
ntea
reset_com
| [ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |