c - Linux C : error of redefinition of in include headers

标签 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的函数原型(prototype):

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/

相关文章:

android - ARM 警告 : swp{b} use is deprecated for this architecture

c - 除非我使用 fseek(fp,0,SEEK_CUR),否则为什么 rb+ 模式下的 R/W 过渡文件中间失败?为什么它在文件末尾工作?

c - ntohs() 问题

C++-Linux : Capturing the mouse ala VirtualBox

python - 在 MAC OS 或 Linux 中创建快捷方式链接文件或文件夹

c++ - gcc -/usr/bin/ld 错误 : cannot find <library> in/usr/local/lib though ldconfig list it, 并将路径添加到 ld.so.conf

c - 对 `sperm' 的 undefined reference

objective-c - 在 uint8_t * 和 char * 之间转换。会发生什么?

c++ - 解析/proc/文件是否安全?

c - O_DIRECT 标志不起作用