将<errno.h>添加到“ my_header.h”会导致错误消息

时间:2019-01-21 16:03:00

标签: errno

我正在尝试从my_func中的errno.h打印'错误号'。如果我直接在my_func.c中包含<errno.h>,一切正常。但是,如果我在<errno.h>中包含"my_header.h",然后在my_func.c中包含"my_header.h",则编译器会吐出错误:

  

src/my_func.c: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'int (*(*)())' [-Wint-conversion] return (print_errno(errno));


  

/usr/include/sys/errno.h:81:15: note: expanded from macro 'errno' #define errno (*__error())

my_func.c:

#include "my_header.h"

int my_func(void)
{
  if (write(5, "Hello, world!", 13) == -1)
     return(print_errno(errno));
}

my_header.h:

#include <errno.h>

int print_errno(int errno);

print_errno.c:

#include "my_header.h"
#include <stdio.h>

int print_errno(int errno)
{
  printf("error number = %d", errno);
  return (-1);

}

为什么会有这个错误?

1 个答案:

答案 0 :(得分:0)

这是因为您已命名参数errno,该参数已由预处理器进行了扩展

 #define errno (*__error())

(errno.h)

所以这个原型

int print_errno(int errno);

扩展到

int print_errno(int (*__error()));

简短修复,请勿调用参数errno

相关问题