Should I declare system call functions in C?

I read this answer: Should I declare a function prototype in C?

My question is more specific:

In a program using system calls like access() , open() , creat() , write() , read() ... Do I have to declare every function of the system call? So does C work? Because I get the following:

 hw1.c: In function 'main': hw1.c:50:9: warning: implicit declaration of function 'access' [-Wimplicit-function-declaration] hw1.c:131:9: warning: implicit declaration of function 'lseek' [-Wimplicit-function-declaration] hw1.c: In function 'writeFile': hw1.c:159:17: warning: implicit declaration of function 'write' [-Wimplicit-function-declaration] 

Basically, it seems that C is angry with every system call function that I use. I'm a little new to C, and this seems odd to me, although I know that I have to declare the functions I'm writing, I would think that C would know the system call functions and I would not need to explicitly declare them in the code.

I need to do something like this:

int access(const char *pathname, int mode);

If so, why does it make sense? I use other languages ​​and should never do this.

+4
source share
3 answers

Yes, you must include the correct header for every call to your system function. You yourself do not write declarations - you are mistaken. Use the headline!

For the functions you are quoting, the corresponding headers are:

 #include <unistd.h> /* Many POSIX functions (but not all, by a large margin) */ #include <fcntl.h> /* open(), creat() - and fcntl() */ 

See POSIX 2008 for the right header for other POSIX features.

The C99 standard requires all functions to be declared or defined before they are used.


For your own functions, you must emulate a "system." There will be a header that declares a function, a source file that implements it, and other source files that use this function. Other source files use the header to get the correct declaration. The implementation file includes a header to ensure that its implementation is consistent with what other source files expect. So, the headline is the glue that holds it all together.

+11
source

You do not need to declare each as such. What you need to do is include the correct header files containing declarations of these functions. For instance:

 #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> 

stdio.h , fcntl.h and unistd.h (assuming you encode * nix) are the ones you need for the functions mentioned. See the documentation (i.e. man lseek ) for details.

+3
source

You should do this in almost all languages ​​except the set of "core" libraries. Java, C #, Perl, etc. They have import or use forms, or make links in assembly configurations, or ... for things outside the main library. C is no different, except that it has very small (that is, Non-existent) "core libraries" - none of the library headers are turned on by default.

#include is what you are looking for. Check the manual page for each of these features and you'll find out which headings you need to include.

+1
source

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


All Articles