'why the code can not compile after define my own structural type

Good morning. I have a problem - I created definition of the myOwnType and I still have a problem with compile. Please help. C language (not C++).

main.c:

#include <stdio.h>
#include "additional.h"
#include "config.h"

int main(int argc, char *argv[])
{

    return 0;
}

config.h:

#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_

#include "additional.h"

typedef struct MyType
{
    int x;
} MyOwnType_t;

#endif /* SRC_CONFIG_H_ */

additional.c:

#include "config.h"

void foo(MyOwnType_t x)
{

}

additional.h:

#ifndef SRC_ADDITIONAL_H_
#define SRC_ADDITIONAL_H_

#include "config.h"

extern void foo(MyOwnType_t x);

#endif /* SRC_ADDITIONAL_H_ */

Compiler generate error: ..\Src\additional.h:13:10: error: unknown type name 'MyOwnType_t'

Help me please.



Solution 1:[1]

Expanding your includes in additional.c gets you:

#ifndef SRC_CONFIG_H_
#define SRC_CONFIG_H_

#ifndef SRC_ADDITIONAL_H_
#define SRC_ADDITIONAL_H_

/* no effect #include "config.h" */
#ifndef SRC_CONFIG_H_
/* not seen because SRC_CONFIG_H_ is already defined here, from above line 2ish */
#endif /* SRC_CONFIG_H_ */

extern void foo(MyOwnType_t x);

#endif /* SRC_ADDITIONAL_H_ */

typedef struct MyType
{
    int x;
} MyOwnType_t;

#endif /* SRC_CONFIG_H_ */

void foo(MyOwnType_t x)
{

}

"no effect" refers to the fact that the second include of config.h meets an already defined reinclusion guard, the content is not seen in this place. The content IS seen from the first include, but later, AFTER the content of additional.h which would need the content from config.h.

Cleaning that up gets you:

/* no effect #include "config.h" */

extern void foo(MyOwnType_t x);


typedef struct MyType
{
    int x;
} MyOwnType_t;


void foo(MyOwnType_t x)
{

}

In the first line, extern void foo(MyOwnType_t x); you use a not yet known type, MyOwnType_t.
The compiler does not like that.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1