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

Is there a replacement for unistd.h for Windows (Visual C)?(是否有适用于 Windows (Visual C) 的 unistd.h 的替代品?)
本文介绍了是否有适用于 Windows (Visual C) 的 unistd.h 的替代品?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在将一个为 Unix 编写的相对简单的控制台程序移植到 Windows 平台 (VisualC++ 8.0).所有源文件都包含不存在的unistd.h".删除它后,我收到了关于缺少 'srandom'、'random' 和 'getopt' 原型的抱怨.我知道我可以替换随机函数,而且我很确定我可以找到/修改 getopt 实现.

I'm porting a relatively simple console program written for Unix to the Windows platform (Visual C++ 8.0). All the source files include "unistd.h", which doesn't exist. Removing it, I get complaints about misssing prototypes for 'srandom', 'random', and 'getopt'. I know I can replace the random functions, and I'm pretty sure I can find/hack-up a getopt implementation.

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

But I'm sure others have run into the same challenge. My question is: is there a port of "unistd.h" to Windows? At least one containg those functions which do have a native Windows implementation - I don't need pipes or forking.

编辑:

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

I know I can create my very own "unistd.h" which contains replacements for the things I need - especially in this case, since it is a limited set. But since it seems like a common problem, I was wondering if someone had done the work already for a bigger subset of the functionality.

在工作中无法切换到不同的编译器或环境 - 我一直在使用 Visual Studio.

Switching to a different compiler or environment isn't possible at work - I'm stuck with Visual Studio.

推荐答案

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

Since we can't find a version on the Internet, let's start one here.
Most ports to Windows probably only need a subset of the complete Unix file.
Here's a starting point. Please add definitions as needed.

#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  */

这篇关于是否有适用于 Windows (Visual C) 的 unistd.h 的替代品?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Returning a pointer of a local variable C++(返回局部变量 C++ 的指针)
Inline function linkage(内联函数联动)
Which is more efficient: Return a value vs. Pass by reference?(哪个更有效:返回值与通过引用传递?)
Why is std::function not equality comparable?(为什么 std::function 不具有可比性?)
C++ overload resolution(C++ 重载解析)
When to Overload the Comma Operator?(什么时候重载逗号运算符?)