我应该在C中声明系统调用函数吗? [英] Should I declare system call functions in C?

查看:119
本文介绍了我应该在C中声明系统调用函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了这个答案:
必须在C中声明函数原型吗?

我的问题更具体:

在使用系统调用如 access() open() creat() write() read() ...我必须声明每个系统调用函数吗? C是如何工作的?因为我收到以下内容:

  hw1.c:在函数'main'中:
hw1.c: 50:9:警告:函数'access'的隐式声明[-Wimplicit-function-declaration]
hw1.c:131:9:警告:函数'lseek'的隐式声明[-Wimplicit-function-declaration]
hw1.c:在函数'writeFile'中:
hw1.c:159:17:warning:隐式声明函数'write'[-Wimplicit-function-declaration]

基本上,C似乎对我使用的每个系统调用函数感到愤怒。
我对C有点新,这对我来说似乎很陌生,即使我知道我必须声明我写的函数,我会认为C会知道系统调用函数,并且不需要我在代码中明确声明它们。



我是否需要这样做:

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



如果是这样,为什么这有意义?我使用其他语言,从来没有这样做过。

解决方案

是的,您应该为每个系统函数包含正确的头文件使。你不要自己写宣言 - 你会弄错他们。使用标题!



对于您引用的函数,相关标题为:

  #include< unistd.h> / *许多POSIX函数(但不是全部,大幅度增加)* / 
#include< fcntl.h> / * open(),creat()和fcntl()* /

请参阅 POSIX 2008 为其他POSIX功能找到正确的标题。



C99标准要求在使用所有函数之前声明或定义所有函数。






你自己的功能,你应该模仿'系统'。将会有一个声明该函数的头文件,实现它的源文件以及使用该函数的其他源文件。其他源文件使用标题来使声明正确。实现文件包含头文件以确保其实现与其他源文件所期望的一致。所以标题是粘在一起的胶。


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.

解决方案

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.

这篇关于我应该在C中声明系统调用函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆