C语言 .h 文件中的重定义错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5430019/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:09:30  来源:igfitidea点击:

Redefinition errors in .h files

c

提问by codereviewanskquestions

//list.h file
typedef struct _lnode{
    struct _lnode *next;
    size_t row;
    size_t column;
    short data;
}lnode;

typedef struct _llist{
    struct _lnode *head;
    size_t size;

}llist;

//matrix.h file
typedef struct _matrix{

    size_t width;
    size_t height;
    size_t k;

    int **data;

}matrix;

//smatrix.h file
#include "list.h"
#include "matrix.h"

typedef struct _smatrix{
    size_t width;
    size_t height;
    size_t k;

    llist data;
}smatrix;

smatrix* make_smatrix(matrix *m);

smatrix.h file includes list.h file and matrix.h files. If I include those header files in smatrix.h file then I get

smatrix.h 文件包括 list.h 文件和 matrix.h 文件。如果我在 smatrix.h 文件中包含这些头文件,那么我得到

 redefinition of 'lnode'. redefinition of '_llist' and redefinition of '_matrix' errors.

If I took those heder files our from smatrix.h file then the error went away but it complains about matrix type in the function parameter. I want to call functions defined in list.h and matrix.h files in smatrix.c file.. What do I do? Thanks in advance..

如果我从 smatrix.h 文件中取出这些 heder 文件,那么错误就会消失,但它会抱怨函数参数中的矩阵类型。我想调用 smatrix.c 文件中的 list.h 和 matrix.h 文件中定义的函数。我该怎么办?提前致谢..

回答by M'vy

Possible problem of multiple inclusions.

多个夹杂物的可能问题。

Try to guard your header files with #ifndefread about it here

尝试通过阅读此处保护您的头文件#ifndef

file list.h

文件列表.h

#ifndef _LISTH_
#define _LISTH_

<your code>

#endif

file matrix.h

文件矩阵.h

#ifndef _MATRIXH_
#define _MATRIXH_

<your code>

#endif

It will prevent you too have redefinitions if you have a loop in header inclusions.

如果标题包含中有循环,它会阻止您进行重新定义。

回答by akira

you need include guardsin your header files.

您需要在头文件中包含警卫

回答by ds27680

Well from your posted code what I think is missing is at the beginning of each *.h file:

好吧,从您发布的代码中,我认为缺少的是每个 *.h 文件的开头:

#ifndef _some_unique_identifier_for_each_header 
#define _some_unique_identifier_for_each_header

...header contents

#endif //_some_unique_identifier_for_each_header

or a

#pragma once 

if your compiler supports it.

如果您的编译器支持它。

Without this, if the header is included multiple times from various sources you get errors relating to redefinition.

如果没有这个,如果从各种来源多次包含标题,您会收到与重新定义相关的错误。

回答by littleadv

You probably include smatrix.hand list.hin some other file. You should avoid that. The usual way to do that is to use include guards.

您可能将smatrix.h和包含list.h在其他文件中。你应该避免这种情况。通常的方法是使用include guards.

These are macros that you check with #ifdefat the beginning of the file (with #endifin the end), and #definethem inside the #ifdef ... #endif, thus insuring that even if you do include the same file multiple times, the compiler will only read it once, at the first time, and skip all the rest.

这些都是宏,你检查#ifdef的文件的开头(与#endif中端),以及#define它们内部#ifdef ... #endif,从而确保即使你不使用相同的文件多次,编译器将只读取一次,在第一次时间,并跳过所有其余的。