我正在尝试用 C 编写一个简单的 shell。但我无法使用 sys/wait.h。我的代码相同:
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
char command[BUFSIZ];
int status;
pid_t pid;
for(;;) {
printf("simpsh: ");
if(fgets(command, sizeof(command), stdin) == NULL) {
printf("\n");
return 0;
}
command[strlen(command) - 1] = '\0';
if((pid = fork()) == 0)
execlp(command, command, 0);
while(wait(&status) != pid)
continue;
printf("\n");
}
}
我正在使用 dev-C++,结果是:
[Error] sys/wait.h: No such file or directory
这里有什么问题吗?如果有人对我用 C 或 C++ 创建一个简单的 shell 有一些建议,请给我一些链接或代码。谢谢!
来自 http://sourceforge.net/p/dev-cpp/discussion/48211/thread/e32720b4
TextTiling/util.c:8:22:sys/wait.h:没有这样的文件或目录 TextTiling/util.c:26:21:sys/vfs.h:没有这样的文件或目录
These are OS specific files, but are not Windows headers.
Windows 不完全支持 POSIX API。您可能需要对代码进行一些移植才能使其在 Windows 上编译和运行。
您可能会更幸运地在其预期环境(也许是 Linux 或 OSX,或者类似 UNIX 的环境)中构建它。您也许可以在 Cygwin 下构建它,但这只是一个大锤。
一个简单的解决方案是在虚拟机下运行 Linux(例如使用 VMWare 的免费 VMPlayer 或 VMServer 工具)。然后在虚拟机中构建并运行代码。 VM 可以通过虚拟网络访问 Windows 计算机,因此如果您愿意,您仍然可以在 Windows 环境中获取结果和数据。
在另一个问题上,当项目位于其安装目录 (C:\Dev-Cpp) 的子文件夹中时,Dev-C++ 有时会(显然是随机地)失败。