C 头文件导致警告 "ISO C requires a translation unit to contain at least one declaration"
C header file is causing warning "ISO C requires a translation unit to contain at least one declaration"
我使用 Qt Creator 制作了这些纯 C 文件只是为了测试我的理解:
main.c
#include <stdio.h>
#include "linked.h"
int main()
{
printf("Hello World!\n");
printf("%d", linked());
return 0;
}
linked.h
#ifndef LINKED_H_
#define LINKED_H_
int linked(void);
#endif // LINKED_H
linked.c
int linked()
{
return 5;
}
IDE 在 #define LINKED_H_
和 int linked(void);
之间的 linked.h
行显示警告,内容为
ISO C requires a translation unit to contain at least one declaration
我对这意味着什么的最佳猜测是任何头文件或其他 C 文件,如果它在项目中,应该至少在主文件中的某个地方使用一次。我试过搜索警告,但如果其他地方已经回答了这个问题,我就无法理解答案。在我看来,我已经使用了 linked
函数,所以它不应该给我这个警告。谁能解释一下这是怎么回事?
程序编译和运行完全符合预期。
你写代码的方式,需要用到:
extern int linked(void);
(注意附加的 "extern")。这可能有助于解决问题。
此外,linked.c中的代码应该是:
int linked(void)
{
return 5;
}
(注意 "parameter" - "void")。
根据 IBM,您需要在头文件中进行一些声明,但您确实有。也许 LINKED_H_
是在别处定义的,或者编译器发现预编译器条件可能会导致空解析。
也许这个头文件适合您:
linked.h
#ifndef LINKED_H_
#define LINKED_H_
int linked(void);
#endif // LINKED_H
char __allowLinkedHToBeIsoCCompliant = 1;
我认为问题在于您没有 #include "linked.h"
来自 linked.c
。当前 linked.c
文件没有任何声明;它只有一个函数定义。
要解决此问题,请将此行添加到 linked.c
:
#include "linked.h"
我不知道为什么它说这是linked.h
的问题,但你指出的行号恰好是结尾的行号似乎很巧合linked.c
.
当然,可能仅此而已;巧合。所以,如果这不起作用,请尝试在此文件中放置某种外部声明。最简单的方法是包含标准 header,例如 stdio.h
。不过,我仍然建议您从 linked.c
内部 #include "linked.h"
。
添加一个header
#ifndef LINKED_H_
#define LINKED_H_
#include <stdio.h>
int linked(void);
#endif // LINKED_H
我使用 Qt Creator 制作了这些纯 C 文件只是为了测试我的理解:
main.c
#include <stdio.h>
#include "linked.h"
int main()
{
printf("Hello World!\n");
printf("%d", linked());
return 0;
}
linked.h
#ifndef LINKED_H_
#define LINKED_H_
int linked(void);
#endif // LINKED_H
linked.c
int linked()
{
return 5;
}
IDE 在 #define LINKED_H_
和 int linked(void);
之间的 linked.h
行显示警告,内容为
ISO C requires a translation unit to contain at least one declaration
我对这意味着什么的最佳猜测是任何头文件或其他 C 文件,如果它在项目中,应该至少在主文件中的某个地方使用一次。我试过搜索警告,但如果其他地方已经回答了这个问题,我就无法理解答案。在我看来,我已经使用了 linked
函数,所以它不应该给我这个警告。谁能解释一下这是怎么回事?
程序编译和运行完全符合预期。
你写代码的方式,需要用到:
extern int linked(void);
(注意附加的 "extern")。这可能有助于解决问题。
此外,linked.c中的代码应该是:
int linked(void)
{
return 5;
}
(注意 "parameter" - "void")。
根据 IBM,您需要在头文件中进行一些声明,但您确实有。也许 LINKED_H_
是在别处定义的,或者编译器发现预编译器条件可能会导致空解析。
也许这个头文件适合您:
linked.h
#ifndef LINKED_H_
#define LINKED_H_
int linked(void);
#endif // LINKED_H
char __allowLinkedHToBeIsoCCompliant = 1;
我认为问题在于您没有 #include "linked.h"
来自 linked.c
。当前 linked.c
文件没有任何声明;它只有一个函数定义。
要解决此问题,请将此行添加到 linked.c
:
#include "linked.h"
我不知道为什么它说这是linked.h
的问题,但你指出的行号恰好是结尾的行号似乎很巧合linked.c
.
当然,可能仅此而已;巧合。所以,如果这不起作用,请尝试在此文件中放置某种外部声明。最简单的方法是包含标准 header,例如 stdio.h
。不过,我仍然建议您从 linked.c
内部 #include "linked.h"
。
添加一个header
#ifndef LINKED_H_
#define LINKED_H_
#include <stdio.h>
int linked(void);
#endif // LINKED_H