Rasmi Ranjan Nayak
Rasmi Ranjan Nayak

Reputation: 11968

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 error that undefined reference to sleep() and / * delay also* /.

Upvotes: 11

Views: 42723

Answers (6)

kimoduor
kimoduor

Reputation: 614

I also struggled a bit with this, install Cygwin from https://www.cygwin.com/ Make sure you check gcc-core and gcc-debug info packages from the package window.

enter image description here

Install the packages from the internet, then change the default Compiler to Cygwin GCC from the codeblock as demonstrated below enter image description here

Confirm whether the Compiler installation directory is well placed, if not, click Autodetect button as shown below: enter image description here

Upvotes: 0

cha0site
cha0site

Reputation: 10737

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.

Upvotes: 10

Praveen Kishor
Praveen Kishor

Reputation: 2523

There is no fork() system call in windows, instead you can try cygwin or pthreads.

Upvotes: 0

Seeder
Seeder

Reputation: 113

You try using cygwin Also I am not sure, but there's this equivalent called system call spawn() in ms-dos environment. you may try using that.

Upvotes: 0

Seeder
Seeder

Reputation: 113

fork() is a unix system call, so it will definitely produce an undefined reference when you do this in windows OS. Windows does not support fork().

Upvotes: 2

Fred Foo
Fred Foo

Reputation: 363807

There is no fork system call on Windows.

Upvotes: 3

Related Questions