首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有用于Windows (Visual C)的unistd.h的替代品?

是否有用于Windows (Visual C)的unistd.h的替代品?
EN

Stack Overflow用户
提问于 2008-12-04 19:51:08
回答 10查看 355.9K关注 0票数 205

我正在将一个为Unix编写的相对简单的控制台程序移植到Windows平台(Visual C++ 8.0)。所有的源文件都包含"unistd.h",它并不存在。删除它后,我收到了关于“srandom”、“random”和“getopt”原型丢失的抱怨。我知道我可以替换随机函数,并且我非常确定我可以找到/hack-up一个getopt实现。

但我相信其他人也遇到了同样的挑战。我的问题是: Windows有没有"unistd.h“的端口?至少有一个包含那些具有本机Windows实现的函数-我不需要管道或forking。

编辑

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

在工作中切换到不同的编译器或环境是不可能的-我坚持使用Visual Studio。

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2009-05-05 17:26:51

由于我们在互联网上找不到一个版本,让我们在这里开始一个。

大多数到Windows的端口可能只需要完整Unix文件的一个子集。

这是一个起点。请根据需要添加定义。

代码语言:javascript
运行
复制
#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  */
票数 206
EN

Stack Overflow用户

发布于 2009-11-19 06:56:15

尝试包含io.h文件。它似乎是Visual Studio的unistd.h的等价物。

我希望这能帮到你。

票数 122
EN

Stack Overflow用户

发布于 2008-12-04 20:29:03

我建议使用mingw/msys作为开发环境。尤其是如果您正在移植简单的控制台程序。Msys在Windows上实现了一个类似Unix的shell,mingw是GNU compiler collection (GCC)和其他GNU构建工具在Windows平台上的一个移植。它是一个开源项目,非常适合这项任务。我目前使用它为Windows XP构建实用程序和控制台应用程序,并且它肯定具有您正在寻找的unistd.h头文件。

安装过程可能有点棘手,但我发现从开始安装是最好的。

票数 25
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/341817

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档