CinePaint

Deep paint and other open source software tools for motion picture retouching and HDR photography with 8, 16 and 32-bit per channel color.

Tuesday, February 05, 2008

What Is Libunistd?

Libunistd is a very thin POSIX wrapper created by Robin Rowe that enables building typical Linux code on Windows with VC++. Using libunistd, many Linux terminal-based apps will build on Windows as-is or with only minor changes.

Supported POSIX Calls...

Libunistd supports open/read/write/close, dlopen, opendir, gettimeofday, and the typical BSD socket calls accept/bind/connect/listen/select/socket. It also allows iostream.h and fstream.h to substitute for their non-dot-h counterparts in VC++.

Unsupported POSIX Calls...

The most notable omission is fork. Portable programs can be revised to handle POSIX threads (pthread) instead. A Windows pthreads port is available from Red Hat or there's a chopped down lite version I created in CinePaint CVS. Also missing is fancy signal handling. And, ioctls are obviously not portable.

How to Use...

To use libunistd simply #include <unistd.h> in your code and set your Windows build path to find it. You should not need any #ifdef WIN32 code in your app. The whole point of libunistd is to eliminate that junk.

You'll want to statically link to the (tiny) libunistd library. That has implementations of opendir and gettimeofday. You'll also need to link to Windows' Winsock library and possibly its Common Controls library. If you're running sockets from a Windows command-line app, there's a magic start-up function called StartWinsock() you'll need to call.

How It Works...

Many POSIX calls are available in Windows to support its implementation of BSD sockets, but have non-standard header names. There are many deliberately empty POSIX headers in libunistd so that Windows won't complain about missing Linux POSIX headers. Most of Windows POSIX implementation is in <winsock.h>, but you don't need to know that. Just include unistd.h and it will straighten out the Windows mess. Including unistd.h on Linux does no harm. That this file is standard on nix systems but not present on Windows is the trick to making code portable.

Alternatives...

Note that libunistd is deliberately a very thin implementation covering only the most basic POSIX calls that everyone uses. If you want a full-featured POSIX implementation and don't care how cumbersome it is, you're looking for Cygwin. If you're looking for a thin implementation of nix shell tools (ls, diff, du...) then you want CoreUtils for Windows, not Cygwin. With CoreUtils you can handle patching and other nix tasks in Windows using the standard nix command-line tools. By the way, if you're looking for a nice free Windows Perl you want PxPerl. A nice free Windows text editor that handles unix-style line feeds is Notepad++.

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home