Headers for C POSIX Functions

Where or how can I find the correct C headers for inclusion in a C ++ program to get the declaration of C functions declared in a POSIX compatible environment?

I ask this because I need to use the open() system call in my C ++ program for my purposes, so I first tried to include the headers mentioned in the open() online documentation (in the SYNOPSIS section), which sys/stat.h and fcntl.h . However, when trying to compile, the compiler complained that open() not declared. After searching on google, I discovered that another possibility was unistd.h . I tried using this header and the program compiled. So I went back to the POSIX documentation to learn more about unistd.h , to check if open() was listed there, but I couldn't find anything about it.

What am I doing wrong? Why is there such a mismatch between the POSIX documentation and my GCC environment?

+6
source share
1 answer

On my Linux Debian / Sid, the man 2 open page says:

 SYNOPSIS #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> 

So, you need to include all three of the above files . And open declared in /usr/include/fcntl.h , but a declaration of the other two is required.

And the next test file

 /* file testopen.c */ #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int testopen (void) { return open ("/dev/null", O_RDONLY); } 

compiles with gcc -Wall -c testopen.c without any warning.

+8
source

Source: https://habr.com/ru/post/901256/


All Articles