C:隐含的功能声明

kub*_*j21 16 c sockets implicit-declaration

我正在开发一个我们正在开发自己的RPC客户端的任务.编译我的服务器部分后,我收到以下警告:

implicit declaration of function 'read'
implicit declaration of function 'write'
Run Code Online (Sandbox Code Playgroud)

我明白如果我要在主要的ex之后创建一个函数,我通常会收到此警告:

int main() {
    doSomething();
}

void doSomething() {
    ...
}
Run Code Online (Sandbox Code Playgroud)

在上面的例子中,它应该抱怨我创建"doSomething"的功能.

为什么我的编译器会抱怨系统调用是隐式声明的,当它出现在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*/
Run Code Online (Sandbox Code Playgroud)

oua*_*uah 50

#include <unistd.h>在程序中添加include指令.

readwrite函数中声明unistd.h,你需要你的函数声明之前,能够给他们打电话.

  • @ouah:C99和C11禁止没有可见声明的函数调用,但违反该规则只需要编译器发出诊断消息.警告符合条件.特别是gcc,打印出很多标准所说的违反约束*的警告.这是合法的,但恕我直言不幸(我更喜欢gcc默认更严格).您可以通过给出`-pedantic-errors`选项并指定标准(`-std = c90` /`-ansi`,`-std = c99`,`-std = c1x /`std = c11`来使其更严格). (2认同)