C语言 C: 函数的隐式声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16178876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C: Implicit declaration of function
提问by kubiej21
I am working on an assignment in which we are developing our own RPC client. Upon compiling my server portion, I receive warnings for the following:
我正在处理一项我们正在开发我们自己的 RPC 客户端的任务。编译我的服务器部分后,我收到以下警告:
implicit declaration of function 'read'
implicit declaration of function 'write'
I understand that I would typically receive this warning if I were to create a function following my main, ex:
我知道,如果我要在 main 之后创建一个函数,我通常会收到此警告,例如:
int main() {
doSomething();
}
void doSomething() {
...
}
In the above case, it should complain about the function that I created "doSomething".
在上述情况下,它应该抱怨我创建的“doSomething”函数。
Why then would my compiler complain that a system call was declared implicitly, when it appears in a function that was declared before the main? Below is the function in which the system call appears.
那么为什么我的编译器会抱怨系统调用是隐式声明的,当它出现在 main 之前声明的函数中时?下面是出现系统调用的函数。
void Open(int connfd) {
/*Get message size*/
unsigned char temp[4] = { 0 };
int n = read(connfd, temp, 4);
if(n < 0) {/*On error*/
perror("Read error");
exit(1);
}/*End if*/
unsigned int msgSize = temp[0] +
(temp[1] * 256) +
(temp[2] * 256 * 2) +
(temp[3] * 256 * 3);
printf("msgSize = %d\n", msgSize);
/*Allocate memory for message*/
char * msg = malloc(msgSize);
if(msg == NULL) {
perror("Allocation error");
exit(1);
}/*End if*/
msg = memset(msg, 0, msgSize);
/*Read entire message from client*/
n = read(connfd, msg, msgSize);
if(n < 0) {/*On error*/
perror("Read error");
exit(1);
}/*End if*/
/*Extract pathname from message - NULL terminated*/
char * pathname = malloc(strlen(msg) + 1);
if(pathname == NULL) {
perror("Allocation error");
exit(1);
}/*End if*/
pathname = memset(pathname, 0, strlen(msg) + 1);
pathname = memcpy(pathname, msg, strlen(msg));
/*Extract flags from message*/
int i;
for(i = 0; i < sizeof(int); i++) {
temp[i] = msg[strlen(pathname) + 1 + i];
}/*End for i*/
unsigned int flags = temp[0] +
(temp[1] * 256) +
(temp[2] * 256 * 2) +
(temp[3] * 256 * 3);
/*Extract mode from message*/
for(i = 0; i < sizeof(mode_t); i++) {
temp[i] = msg[strlen(pathname) + 1 + sizeof(int) + 1 + i];
}/*End for i*/
mode_t mode = temp[0] +
(temp[1] * 256) +
(temp[2] * 256 * 2) +
(temp[3] * 256 * 3);
free(msg);/*Free msg since it is no longer needed*/
/*Open pathname*/
umask(0);
int fd = open(pathname, flags, mode);
free(pathname);/*Free pathname since it is no longer needed*/
/*Prepare response*/
char * response = malloc(sizeof(int) * 2);
if(response == NULL) {
perror("Allocation error");
exit(1);
}/*End if*/
response = memset(response, 0, sizeof(int) * 2);
/*Build return message*/
memcpy(&response[0], &fd, sizeof(fd));
memcpy(&response[4], &errno, sizeof(fd));
/*Can't guarante socket will accept all we try to write, cope*/
int num, put;
int left = sizeof(int) * 2; put = 0;
while(left > 0) {
if((num = write(connfd, response + put, left)) < 0) {
perror("inet_wstream: write");
exit(1);
} else {
left -= num;
put += num;
}/*End else*/
}/*End while*/
free(response);/*Free response since it is no longer needed*/
return;
}/*End Open*/
回答by ouah
Add #include <unistd.h>include directive in your program.
#include <unistd.h>在您的程序中添加include 指令。
readand writefunctions are declared in unistd.hand you need a declaration of your functions before to be able to call them.
read和write函数被声明在,unistd.h并且您需要先声明您的函数才能调用它们。
回答by Jens
Implicit function declarationsare those that the compiler sees the first time used as a function call (as opposed to those where a prototype or function definition is seen first).
隐式函数声明是编译器第一次看到用作函数调用的声明(与首先看到原型或函数定义的声明相反)。
"System calls" are no exception to this rule, since the C Standard(s) don't make a distinction between "ordinary functions" and "system calls". You likely forgot to include the relevant header providing the prototype (unistd.h).
“系统调用”也不例外,因为 C 标准不区分“普通函数”和“系统调用”。您可能忘记包含提供原型的相关标头 ( unistd.h)。

