我有一个项目,其标题包含以下 map :

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

util.h 中,我定义了的函数原型(prototype)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{
        ^

最佳答案

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

#ifndef UTIL_H_INCLUDED
#define UTIL_H_INCLUDED

/* header contents goes here */

#endif /* UTIL_H_INCLUDED */

关于c - Linux C : error of redefinition of in include headers,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37870090/

10-12 21:07