gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 11:31:43 26 4
gpt4 key购买 nike

我有一个项目,它有下面的标题包括 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com