2008-12-04 204 views
131

我將一個爲Unix編寫的相對簡單的控制檯程序移植到Windows平臺(Visual C++ 8.0)。所有源文件都包含「unistd.h」,它不存在。刪除它後,我收到有關'srandom','random'和'getopt'原型的錯誤投訴。 我知道我可以替換隨機函數,我很確定我可以找到/ hack-up getopt實現。是否有替代Windows的unistd.h(Visual C)?

但我相信其他人也遇到了同樣的挑戰。 我的問題是:是否有一個「unistd.h」的Windows端口?至少有一個包含那些具有本地Windows實現的函數 - 我不需要管道或分支。

編輯

我知道我可以創建自己的「unistd.h中」,其中包含了我所需要的東西的替代品 - 尤其是在這種情況下,因爲它是一組有限的。但是,由於它看起來像是一個常見問題,我想知道是否有人已經完成了這項功能的更大子集的工作。

切換到不同的編譯器或環境在工作中是不可能的 - 我被Visual Studio困住了。

回答

134

既然我們無法在互聯網上找到一個版本,讓我們從這裏開始。
到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 */ 
20

我會推薦使用mingw/msys作爲開發環境。特別是如果你正在移植簡單的控制檯程序。 Msys在Windows上實現了一個類Unix的外殼,而mingw是Windows平臺上的GNU compiler collection (GCC)和其他GNU構建工具的一個端口。這是一個開源項目,非常適合這項任務。我目前使用它來爲Windows XP構建實用程序和控制檯應用程序,並且肯定會找到您要查找的unistd.h標題。

安裝過程可能有點棘手,但我發現最好的地方是MSYS

+1

和這些天MSYS2 – 2017-05-08 01:35:52

-1

創建您自己的unistd.h標題幷包含函數原型所需的標題。

+6

我知道我可以做到這一點,我會編輯的問題,使之更清晰。問題的關鍵是:有沒有其他人爲我做過? – AShelly 2008-12-05 20:42:54

5

不,IIRC在Windows上沒有getopt()

Boost,但是,有program_options圖書館......它工作正常。這看起來似乎有點過分,但並不糟糕,特別是考慮到除了命令行選項之外,它還可以處理配置文件和環境變量中的程序選項。

+0

有趣的谷歌搜索「getopt窗口」給這個作爲第一擊:http://www.codeproject.com/KB/cpp/xgetopt.aspx – 2008-12-05 05:20:29

+0

點並不需要重新編寫現有的代碼來使用Boost或一些其他解析器。 – AShelly 2008-12-05 21:52:53

+0

@AShelly:如果你只使用* nix API來編寫代碼,那麼你別無選擇,只能重寫現有的代碼來移植,或者提供你自己的缺失功能的實現。如果您使用boost :: program_options開始,它可以在多個平臺上工作而不用更改。 – paxos1977 2008-12-08 00:15:47

89

嘗試包括io.h文件。它似乎是Visual Studio的相當於unistd.h

我希望這會有所幫助。

13

我試圖找到getpid()(在unistd.h中定義)的Windows備選方案時偶然發現此線程。事實證明,包括process.h有竅門。也許這有助於未來找到這個線索的人。