是否有替代unistd.h的Windows(Visual C)?

我将一个为Unix编写的相对简单的控制台程序移植到Windows平台( Visual C ++ 8.0 )。 所有源文件都包含“unistd.h”,它不存在。 除去它,我得到了有关“srandom”,“random”和“getopt”的错误原型的投诉。 我知道我可以replace随机函数,我很确定我可以find/ hack-up getopt实现。

但我相信其他人也遇到了同样的挑战。 我的问题是:是否有一个“unistd.h”的Windows端口? 至less有一个包含那些具有本地Windows实现的函数 – 我不需要pipe道或分支。

编辑

我知道我可以创build我自己的“unistd.h”,它包含了我需要的东西的替代品 – 特别是在这种情况下,因为它是一个有限的集合。 但是,由于这似乎是一个普遍的问题,我想知道是否有人已经完成了这项function的更大的子集的工作。

切换到不同的编译器或环境是不可能的 – 我坚持使用Visual Studio。

由于我们在互联网上找不到版本,我们从这里开始。
到Windows的大多数端口可能只需要完整的Unix文件的一个子集。
这是一个起点。 请根据需要添加定义。

#ifndef _UNISTD_H #define _UNISTD_H 1 /* This is intended as a drop-in replacement for unistd.h on Windows. * Please add functionality as neeeded. * https://stackoverflow.com/a/826027/1202830 */ #include <stdlib.h> #include <io.h> #include <getopt.h> /* getopt at: https://gist.github.com/ashelly/7776712 */ #include <process.h> /* for getpid() and the exec..() family */ #include <direct.h> /* for _getcwd() and _chdir() */ #define srandom srand #define random rand /* Values for the second argument to access. These may be OR'd together. */ #define R_OK 4 /* Test for read permission. */ #define W_OK 2 /* Test for write permission. */ //#define X_OK 1 /* execute permission - unsupported in windows*/ #define F_OK 0 /* Test for existence. */ #define access _access #define dup2 _dup2 #define execve _execve #define ftruncate _chsize #define unlink _unlink #define fileno _fileno #define getcwd _getcwd #define chdir _chdir #define isatty _isatty #define lseek _lseek /* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call eg closesocket(). */ #ifdef _WIN64 #define ssize_t __int64 #else #define ssize_t long #endif #define STDIN_FILENO 0 #define STDOUT_FILENO 1 #define STDERR_FILENO 2 /* should be in some equivalent to <sys/types.h> */ typedef __int8 int8_t; typedef __int16 int16_t; typedef __int32 int32_t; typedef __int64 int64_t; typedef unsigned __int8 uint8_t; typedef unsigned __int16 uint16_t; typedef unsigned __int32 uint32_t; typedef unsigned __int64 uint64_t; #endif /* unistd.h */ 

尝试包括io.h文件。 这似乎是Visual Studio的相当于unistd.h

我希望这有帮助。

我会build议使用mingw / msys作为开发环境。 特别是如果你正在移植简单的控制台程序。 Msys在Windows上实现了一个类Unix的shell,而mingw是GNU编译器集合(GCC)和其他GNU 编译工具的一个端口。 这是一个开源项目,非常适合这个任务。 我目前使用它来为Windows XP构build实用程序和控制台应用程序,它肯定有你正在寻找的那个unistd.h头文件。

安装过程可能有点棘手,但我发现最好的地方是在MSYS

我偶然发现了这个线程,同时试图findgetpid() (在unistd.h定义getpid()的Windows替代scheme。 事实certificate,包括process.h的窍门。 也许这有助于未来find这个线程的人。

不,IIRC在Windows上没有getopt() 。

但是, Boost有program_options库,它可以正常工作。 起初看起来似乎有点过分,但并不糟糕,特别是考虑到除了命令行选项外,还可以在configuration文件和环境variables中处理设置程序选项。

创build自己的unistd.h头文件,并包含函数原型所需的头文件。