【问题标题】:how to compile fork() on windows [currently trying with cygwin]如何在 Windows 上编译 fork() [目前正在尝试使用 cygwin]
【发布时间】:2020-09-17 06:40:36
【问题描述】:

我正在尝试将为 linux 编写的 c 代码转换为 windows 的 c 代码。 尝试“制作”时,它会说

函数'fork'的隐式声明

所以我搜索了互联网,发现了这个:What is the closest thing Windows has to fork()? 下载 Cygwin 并安装它并再次尝试“make”,它吐出相同的错误输出。之后,我将我的 c 项目复制到 cygwin 文件夹中,打开“cygwin-terminal”程序并在其中运行“make”,仍然是相同的错误输出。 之后,我将 cygwin bin 文件夹中的“cygwin1.dll”复制到我的项目文件夹中,并将我的 Makefile 更改为

gcc -g -Wall -Wextra -Werror -o test_open cygwin1.dll main.c dictionary.c config.c tools.c

然后在普通终端再次启动'make',但它仍然会吐出隐式声明错误。

你有什么建议吗,接下来我可以尝试什么来编译 fork()?

包括:

#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdarg.h>
#include<errno.h>
#include<signal.h>

【问题讨论】:

  • cygwin1.dll gcc -g -Wall -Wextra -Werror -o test_open cygwin1.dll main.c dictionary.c config.c tools.c 的这种用法没有意义。 normal terminal 是什么意思?为什么不是Cygwin Terminal,有什么不同?你能在那儿重复cygcheck -f /usr/include/unistd.h吗?

标签: c linux windows cygwin fork


【解决方案1】:

fork 需要头部

#include <unistd.h>

https://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html

包含在cygwin-devel 包中

$ cygcheck -f /usr/include/unistd.h
cygwin-devel-3.1.4-1

使用示例代码 https://www.geeksforgeeks.org/fork-system-call/

#include <stdio.h> 
#include <sys/types.h> 
#include <unistd.h> 
int main() 
{ 

    // make two process which run same 
    // program after this instruction 
    fork(); 

    printf("Hello world!\n"); 
    return 0; 
} 

编译并运行

$ gcc prova.c -o prova

$ ./prova
Hello world!
Hello world!

【讨论】:

  • 'cygcheck -f /usr/include/unistd.h' 输出 'cygwin-devel-3.1.4-1' 但在 cygwin 中使用 gcc 编译时似乎仍然没有使用 cygwin unistd终端。还有错误:函数'fork'的隐式声明 [-Werror=implicit-function-declaration] int childIdF = fork();
  • 您使用的编译器是什么?你确定你使用的是 cygwin make 和 gcc 吗?复制和粘贴命令行和命令输出
猜你喜欢
  • 1970-01-01
  • 2016-08-19
  • 2011-04-12
  • 1970-01-01
  • 2012-01-01
  • 2017-09-19
  • 1970-01-01
  • 1970-01-01
  • 2013-08-13
相关资源
最近更新 更多