C - gcc/clang: 错误: 冲突的类型<function_name>: 为什么函数名称很重要?



示例代码(t987.c):

void *NAME();
void *NAME(void *, int, unsigned);

调用:

$ gcc t987.c -c -DNAME=memset1
<nothing>
$ gcc t987.c -c -DNAME=memset
<command-line>: error: conflicting types for ‘memset’; have ‘void *(void *, int,  unsigned int)’
t987.c:2:7: note: in expansion of macro ‘NAME’
2 | void *NAME(void *, int, unsigned);
|       ^~~~
<command-line>: note: previous declaration of ‘memset’ with type ‘void *(void *, int,  long unsigned int)’
t987.c:1:7: note: in expansion of macro ‘NAME’
1 | void *NAME();
# clang: the same behavior

问题:为什么函数名很重要?

memset1没有其他定义或声明。所以这两个声明是兼容的:

void *memset1();
void *memset1(void *, int, unsigned);

因为第一个说参数的数量和类型是未知的

然而这给了你一个问题:

void *memset();
void *memset(void *, int, unsigned);

由于memset是由C标准定义的,因此被认为是实现的一部分,因此它也在内部声明为:

void *memset(void *, int,  long unsigned int)

与第二个声明冲突。

最新更新