简体   繁体   中英

Including header file causes conflicting types

So I have this struct in a header file, called it h1.h

typedef struct{
    int a;
    int b;
    special_t test;
} randomDataType

So I include the header file that contains the declaration of special_t. None of the functions in either header file are named the same, and I don't see how circular dependencies could come into play since the functions declared in the second header file are only ever accessed through the functions declared in h1.h

Yet I have a 'previous declaration of _ _' error for everything in the other header. Why is this?

It sounds like you do not have multiple inclusion guards on your header files. Except in very rare cases which you will know when you get to them, C header files should be written like this:

/* This is h1.h.
   This is its license.

   This is what you will find here. */

#ifndef H1_H__
#define H1_H__

all declarations go here

#endif /* h1.h */

There should not be anything outside the #ifndef ... #endif block except for comments, and you need to pick the macro name so that it won't conflict with any other header -- assume that all C header files have this construct in them, or something very like it.

What this does is allow you to #include "h1.h" as many times as you like; the compiler will only process its contents once. Many compilers recognize this construct and won't even open the file again (as long as the macro is still defined). Thus, you can safely include headers from other headers (that might need only a few of the declarations) without worrying about what any other file has done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM