Reputation: 7952
I read this answer: Must declare function prototype in C?
My question is more specific:
In a program that uses system calls like access()
, open()
, creat()
, write()
, read()
... Must I declare every system call function? Is that how C works? Because I'm getting 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 C is angry with every system call function I'm using. I am somewhat new to C and this appears strange to me even though I know I have to declare functions I write I would think C would know the system call functions and would not need me to declare them explicitly in the code.
Do I need to do something like this:
int access(const char *pathname, int mode);
If so why does that make sense? I use other languages and never have to do this.
Upvotes: 4
Views: 13475
Reputation: 753695
Yes, you should include the correct header for every system function call you make. You don't write the declarations yourself—you'll get them wrong. Use the header!
For the functions you cite, the relevant 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 to find the correct header for other POSIX functions.
The C99 standard requires that all functions are declared or defined before they are used.
For your own functions, you should emulate the 'system'. There will be a header that declares the function, the source file that implements it, and the other source files that use the function. The other source files use the header to get the declaration correct. The implementation file includes the header to make sure its implementation is consistent with what the other source files expect. So the header is the glue that holds it all together.
Upvotes: 12
Reputation: 3650
You do not have to declare each one, per se. What you need to do is include the proper header files that contain the declarations of these functions. For example:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
stdio.h
, fcntl.h
, and unistd.h
(assuming you are coding for *nix) are the ones you need for the functions you mentioned. For specifics, consult your documentation (i.e. man lseek
).
Upvotes: 3
Reputation: 206689
You have to do that in almost all languages except for a set of "core" libraries. Java, C#, Perl, etc. all have forms of import
or use
, or making references in build configurations, or... for things outside the core library. C is no different, except that it has a very small (i.e. non-existent) "core libraries" - none of the library headers get included by default.
#include
is what you're looking for. Check the man page for each of these functions, and you'll know what headers you need to include.
Upvotes: 1