簡體   English   中英

Linux C:重新定義包含標頭中的錯誤

[英]Linux C: error of redefinition of in include headers

我有一個項目,其標題包含以下地圖:

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{
        ^

您需要在頭文件中添加包含保護 ,以防止多次包含其內容。 例:

#ifndef UTIL_H_INCLUDED
#define UTIL_H_INCLUDED

/* header contents goes here */

#endif /* UTIL_H_INCLUDED */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM