Linux C:重新定义包含标头的错误

时间:2016-06-16 22:02:42

标签: c linux gcc compiler-errors

我有一个项目,其下面的标题包括map:

main.c <- main.h <- tcphelper.h <- tcptest.h <- util.h
                 <- udptest.h    <------------- util.h

util.h 中,我定义了 struct cpu_usage 的函数原型:

void get_cpu_usage(struct cpu_usage *cu);

现在当我通过GCC编译这个项目时,我有这个重新定义错误。怎么解决这个问题?

谢谢!

In file included from udptest.h:15:0,
                 from main.h:10,
                 from main.c:7:
util.h:27:8: error: redefinition of struct cpu_usage
 struct cpu_usage{
        ^
In file included from tcptest.h:14:0,
                 from tcphelper.h:10,
                 from main.h:9,
                 from main.c:7:
util.h:27:8: note: originally defined here
 struct cpu_usage{
        ^

1 个答案:

答案 0 :(得分:6)

您需要在标头文件中添加Include guards,以防止多次包含其内容。例如:

#ifndef UTIL_H_INCLUDED
#define UTIL_H_INCLUDED

/* header contents goes here */

#endif /* UTIL_H_INCLUDED */