11

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.

kame
  • 20,848
  • 33
  • 104
  • 159
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • @Vlad: Windows isn't "broken by design"; it just doesn't care how \*nix does things. It had its own problems to solve and solved them its own way, which -- although not the way a \*nix wonk might've liked -- works well enough to have powered the vast majority of the computing world for decades. Well enough, in fact, that a bunch of stuff it's done out of the box for 15+ years has been reinvented or bolted on for Linux and \*BSD. (Let's see... Registry? Real ACLs? Native threading? Binary compatibility? Oh, wait, \*nix *still* doesn't have that one...) – cHao Jul 09 '12 at 03:02

6 Answers6

10

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.

cha0site
  • 10,517
  • 3
  • 33
  • 51
  • Can we do UNIX/LINUX programming Cygwin? Will it give complete Linux programming flavor? – Rasmi Ranjan Nayak Jan 11 '12 at 13:36
  • Almost complete. You're still running on Windows. But it should be sufficient. There's a [`cygwin` FAQ](http://cygwin.com/faq/faq-nochunks.html) that answers some common questions. – cha0site Jan 11 '12 at 13:42
  • @cha0site: Does Cygwin also implement `clone ()`? Or "epoll" API? BTW, I saw a project somewhere some long time ago that was able to run Linux kernel as part of Windows kernel. That was cool and almost full flavor. Not sure where it stands now though. –  Jan 11 '12 at 17:46
  • No `clone()`, and no `epoll`. But you don't always need those. And the Linux kernel inside the Windows kernel is [coLinux](http://www.colinux.org/), I believe. Dan Aloni is indeed a most 1337 h4x0r. Also check out [UMLWin32](http://umlwin32.sourceforge.net/) for another one of his crazy projects. – cha0site Jan 11 '12 at 19:09
  • @VladLazarenko: And check out [andLinux](http://www.andlinux.org/) for a working coLinux distro. – cha0site Jan 11 '12 at 19:11
3

There is no fork system call on Windows.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • 1
    There could be in some libraries, like Cygwin. –  Jan 11 '12 at 13:07
  • @larsmans: Does windows has any other system call which can replace fork() and sleep()? – Rasmi Ranjan Nayak Jan 11 '12 at 13:08
  • 1
    There's [CreateProcess](http://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx), but the win32api is completely different from the POSIX API. – cha0site Jan 11 '12 at 13:11
  • 1
    @RasmiRanjanNayak: Windows (at least NT/2000) has an equivalent of `fork ()`. The function is called `NtCreateProcess` or `ZwCreateProcess`. However, this is a native API and not Win32 API. You can refer to "Windows Nt/2000 Native Api Reference" for more details, it even has an example of using it. Also, there is a Win32 `CreateProcess` which can come handy, but not in your case. See this as well - http://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork –  Jan 11 '12 at 13:14
2

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().

Seeder
  • 113
  • 7
0

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

Praveen Kishor
  • 2,413
  • 1
  • 23
  • 28
0

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

kimoduor
  • 504
  • 6
  • 16
0

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.

Seeder
  • 113
  • 7