Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to fork() in Code::Blocks editor in Windows OS

When I am running below code in Code::Blocks in Windows OS. I used to get an error called undefined reference to fork(). I did set/choose GCC compiler as my default compiler.

#include<stdio.h>       
#include<unistd.h>          
void main()          
{       
 int x;       
 x = 0;       
 fork();       
 x = 1;        
 ...     
 ....    
}

Please help me and tell me, can I right unix/linux programs in Code::Blocks in windows environment?

And I write another program,

main()
{
  int x = 0;
  if(x == 0)
  {
    printf("X = %d", x);
    sleep(1000);//used delay also
    x = 1;
    printf("Now X = %d", x);;
  }
}

Here it gives eroor that undefined reference to sleep() and / * delay also* /.
Please help me.

like image 959
Rasmi Ranjan Nayak Avatar asked Jan 11 '12 12:01

Rasmi Ranjan Nayak


People also ask

Can you use fork () in Windows?

Microsoft Windows does not support the fork-exec model, as it does not have a system call analogous to fork() . The spawn() family of functions declared in process.

How do I run a fork file in Windows?

You can't use fork() in a Windows environment, it is is only present in the standard libraries on Unix or Linux based operating systems. The Windows C equivalent are the various spawn() functions.

Which library is fork in C++?

The C library defines fork() . It is the UNIX/Linux-specific system calls to create a process, on linux etc.


2 Answers

No, you can't write Unix code on Windows like that, no matter what IDE you use. However, you should try cygwin, and that should provide the compatibility layer you need.

2017 update: These days we also have Windows Subsystem for Linux.

like image 84
cha0site Avatar answered Sep 19 '22 16:09

cha0site


There is no fork system call on Windows.

like image 26
Fred Foo Avatar answered Sep 19 '22 16:09

Fred Foo