Windows (Visual C) 是否有 unistd.h 的替代品?
我正在将一个为 Unix 编写的相对简单的控制台程序移植到 Windows 平台(Visual C++ 8.0 )。 所有源文件都包含“unistd.h”,但该文件不存在。 删除它后,我收到关于缺少“srandom”、“random”和“getopt”原型的抱怨。 我知道我可以替换随机函数,并且我很确定我可以找到/修改 getopt 实现。
但我确信其他人也遇到过同样的挑战。 我的问题是:Windows 上有“unistd.h”端口吗? 至少有一个包含那些具有本机 Windows 实现的函数 - 我不需要管道或分叉。
编辑:
我知道我可以创建自己的“unistd.h”,其中包含我需要的东西的替代品 - 特别是在这种情况下,因为它是一个有限的集合。 但由于这似乎是一个常见问题,我想知道是否有人已经完成了更大的功能子集的工作。
在工作中不可能切换到不同的编译器或环境 - 我只能使用 Visual Studio。
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 missing 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.
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 containing those functions which do have a native Windows implementation - I don't need pipes or forking.
EDIT:
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.
Switching to a different compiler or environment isn't possible at work - I'm stuck with Visual Studio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
由于我们在互联网上找不到版本,所以我们从这里开始一个。
大多数 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.
尝试包含 io.h 文件。 它似乎是 Visual Studio 中的
unistd.h
的等价物。Try including the
io.h
file. It seems to be the Visual Studio's equivalent ofunistd.h
.我建议使用 mingw/msys 作为开发环境。 特别是如果您正在移植简单的控制台程序。 Msys 在 Windows 上实现了类 Unix 的 shell,mingw 是 GNU 编译器集合 (GCC) 的端口以及 Windows 平台上的其他 GNU 构建工具。 它是一个开源项目,非常适合这项任务。 我目前使用它为 Windows XP 构建实用程序和控制台应用程序,它肯定具有您正在寻找的
unistd.h
标头。安装过程可能有点棘手,但我发现最好的起点是 MSYS。
I would recommend using mingw/msys as a development environment. Especially if you are porting simple console programs. Msys implements a Unix-like shell on Windows, and mingw is a port of the GNU compiler collection (GCC) and other GNU build tools to the Windows platform. It is an open-source project, and well-suited to the task. I currently use it to build utility programs and console applications for Windows XP, and it most certainly has that
unistd.h
header you are looking for.The install procedure can be a little bit tricky, but I found that the best place to start is in MSYS.
我在尝试寻找
getpid()
(在unistd.h
中定义)的 Windows 替代方案时偶然发现了这个线程。 事实证明,包含process.h
就可以解决问题。 也许这对将来找到这个线程的人有帮助。I stumbled on this thread while trying to find a Windows alternative for
getpid()
(defined inunistd.h
). It turns out that includingprocess.h
does the trick. Maybe this helps people who find this thread in the future.不,IIRC Windows 上没有 getopt()。
然而,Boost 具有program_options 库...工作正常。 乍一看似乎有点矫枉过正,但这并不可怕,特别是考虑到除了命令行选项之外,它还可以处理配置文件和环境变量中的程序选项设置。
No, IIRC there is no getopt() on Windows.
Boost, however, has the program_options library... which works okay. It will seem like overkill at first, but it isn't terrible, especially considering it can handle setting program options in configuration files and environment variables in addition to command line options.
Windows 上
unistd.h
的等效项是windows.h
The equivalent of
unistd.h
on Windows iswindows.h
是的,有: https://github.com/robinrowe/libunistd
克隆存储库并添加 < code>path\to\libunistd\unistd 到
INCLUDE
环境变量。Yes, there is: https://github.com/robinrowe/libunistd
Clone the repository and add
path\to\libunistd\unistd
to theINCLUDE
environment variable.MinGW 4.x 在
\MinGW\include
、\MinGW\include\sys
和\MinGW\lib\gcc\mingw32\4.6.2\include\ssp
这是 MinGW 版本的代码,作者:Rob萨伏依; 由 Earnie Boyd、Danny Smith、Ramiro Polla、Gregory McGarry 和 Keith Marshall 修改:
此文件需要包含
_mingw.h
,如下所示:其余包含内容应符合您的环境的标准。
MinGW 4.x has unistd.h in
\MinGW\include
,\MinGW\include\sys
and\MinGW\lib\gcc\mingw32\4.6.2\include\ssp
Here is the code for the MinGW version, by Rob Savoye; modified by Earnie Boyd, Danny Smith, Ramiro Polla, Gregory McGarry and Keith Marshall:
This file requires the inclusion of
_mingw.h
, which is as follows:The rest of the includes should be standard to your environment.
在c/cpp文件中键入stdout宏并尝试查找stdout的定义。 然后你会在 corecrt_wstdio.h 中看到 stdout 等的实际宏定义
所以我认为它应该是相当于 Windows 的 POSIX。
rand,srand分别相当于stdlib.h中声明的random,srandom
type stdout macro in the c/cpp file and try to find the definition of stdout. then you will see in the corecrt_wstdio.h actual macro definition of stdout and etc
so then I think it should be the POSIX equivalent to windows.
rand,srand equivalent to random, srandom respectively which declared in stdlib.h
创建您自己的 unistd.h 标头并包含函数原型所需的标头。
Create your own unistd.h header and include the needed headers for function prototypes.