-3

Hi I am running this code on the visual studio 2022 .But it is saying #include <unistd.h> cannot be opened. Basically it is c code which I am running in cpp environment.

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>

int  main()
{
    int id;
    id = fork();
    if (id < 0) {
        printf(" Error \n");
        return (1);
    }
    else if (id == 0)
        printf("Child\n");
    else
        printf("Parent \n");

    return 0;


}

So i am confusing may be all c libraries are not included in cpp language. And in case i run this program in gcc this is saying fork in not defined???

I have tried to run this code on three compilers in dev cpp , visual studio 2022 and gcc but error have been thrown.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 13
    `unistd.h` is not c standard, its a posix header – 463035818_is_not_an_ai Dec 04 '22 at 15:25
  • 2
    The `uni` prefix in `unistd.h` stands for UNIX. My guess is you're building in Windows? It won't have Unix headers, or the `fork` call for that matter. – Some programmer dude Dec 04 '22 at 15:27
  • 3
    anyhow, no, not all c headers are available in C++ – 463035818_is_not_an_ai Dec 04 '22 at 15:27
  • for example for `stdio.h` there is a C++ header available that you should use `cstdio`. – 463035818_is_not_an_ai Dec 04 '22 at 15:28
  • [related](https://stackoverflow.com/q/9612315/509868) – anatolyg Dec 04 '22 at 15:28
  • 5
    C and C++ are two different languages. C++ is not a superset of C. What compiles as C does not necessarily compile as C++ (and vice versa, though thats usually the lesser surprise) – 463035818_is_not_an_ai Dec 04 '22 at 15:28
  • 2
    I think you'll find that if you try compiling as C in the same environment, it still won't work. So the question doesn't address your problem. "Why doesn't this C thing work in C++?" Um, the C thing doesn't work either. – Raymond Chen Dec 04 '22 at 15:31
  • @463035818_is_not_a_number that's a bad example. Better examples are `stdbool.h`, `tgmath.h`, `complex.h`... which don't exist in C++. To some extent `stdatomic.h` and `stdalign.h` can also be included – phuclv Dec 04 '22 at 15:36
  • Related: [https://stackoverflow.com/questions/33675953/best-way-to-get-around-fork-with-mingw-64](https://stackoverflow.com/questions/33675953/best-way-to-get-around-fork-with-mingw-64) – drescherjm Dec 04 '22 at 15:58
  • Also: [https://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork](https://stackoverflow.com/questions/985281/what-is-the-closest-thing-windows-has-to-fork) – drescherjm Dec 04 '22 at 15:59
  • On windows you could use WSL from Visual Studio or VSCode and compile this code directly on linux: [https://learn.microsoft.com/en-us/cpp/build/walkthrough-build-debug-wsl2?view=msvc-170](https://learn.microsoft.com/en-us/cpp/build/walkthrough-build-debug-wsl2?view=msvc-170) – drescherjm Dec 04 '22 at 16:00

1 Answers1

0

unistd.h is a unix file, if you are running this on Windows you can not use that header file.