問題描述
我正在將一個為 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 的替代品?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!