我正在将一个为Unix编写的相对简单的控制台程序移植到Windows平台(Visual C++ 8.0)。所有的源文件都包含"unistd.h",它并不存在。删除它后,我收到了关于“srandom”、“random”和“getopt”原型丢失的抱怨。我知道我可以替换随机函数,并且我非常确定我可以找到/hack-up一个getopt实现。
但我相信其他人也遇到了同样的挑战。我的问题是: Windows有没有"unistd.h“的端口?至少有一个包含那些具有本机Windows实现的函数-我不需要管道或forking。
编辑
我知道我可以创建我自己的"unistd.h“,它包含了我所需要的东西的替代品--特别是在这种情况下,因为它是一个有限的集合。但由于这似乎是一个常见的问题,我想知道是否有人已经为更大的功能子集完成了工作。
在工作中切换到不同的编译器或环境是不可能的-我坚持使用Visual Studio。
发布于 2009-05-05 17:26:51
由于我们在互联网上找不到一个版本,让我们在这里开始一个。
大多数到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 e.g. 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 */
发布于 2009-11-19 06:56:15
尝试包含io.h
文件。它似乎是Visual Studio的unistd.h
的等价物。
我希望这能帮到你。
发布于 2008-12-04 20:29:03
我建议使用mingw/msys作为开发环境。尤其是如果您正在移植简单的控制台程序。Msys在Windows上实现了一个类似Unix的shell,mingw是GNU compiler collection (GCC)和其他GNU构建工具在Windows平台上的一个移植。它是一个开源项目,非常适合这项任务。我目前使用它为Windows XP构建实用程序和控制台应用程序,并且它肯定具有您正在寻找的unistd.h
头文件。
安装过程可能有点棘手,但我发现从开始安装是最好的。
https://stackoverflow.com/questions/341817
复制相似问题