0

Hello, I'm new to C.

I want to "export" and use a typedef struct in other files but it seems that it doesn't works a lot.

I have those kind of errors

unknown type name 'CAN_frame'
storage size of 'CAN_RxMessage' isn't known
invalid use of undefined type 'struct CAN_frame'

Here are my files:

main.h

#include "can.h"
typedef struct
{
    uint16_t STDID;  //ID
    uint8_t IDE;
    uint8_t RTR;     //Request frame ou data frame
    uint8_t DLC;     //Nombre d'octets de données utiles
    uint8_t data[8]; //Tableau de 8 octets de données
}CAN_frame;

can.h

#include "main.h"

can.c

#include "can.h"
CAN_frame CAN_RxMessage;

void reception_CAN(void)
{
    //CAN_RxMessage filled with data
}

Of course I also want to use this CAN_RxMessage filled with data in my main.c (to send it with the usart to my computer).

I tried to use extern, extern struct, struct and manualy defined CAN_frame in my can.c and can.h(but I think it will only overload or redefine CAN_frame in my main.c so it seems useless).

11
  • 5
    So main.h includes can.h and can.h includes main.h. Recall that inclusion basically dumps file content in place (as text). How is the circular inclusion supposed to work? Commented Apr 28, 2021 at 19:28
  • 3
    It looks to me like can.h includes main.h, and main.h includes can.h. The pre-processor will probably produce an error about an infinite-loop of include files. Commented Apr 28, 2021 at 19:28
  • 2
    Learn about how to use header guards to prevent multiple inclusion and inclusion loops. Commented Apr 28, 2021 at 19:29
  • 1
    See en.wikipedia.org/wiki/Include_guard Commented Apr 28, 2021 at 19:30
  • 2
    Even with header guards, it is often problematic to have circular dependencies among headers. Where such dependencies appear, they can and should be resolved by appropriate refactoring. Commented Apr 28, 2021 at 19:33

1 Answer 1

1

Here is it.

can.h

typedef struct _CAN_frame
{
    uint16_t STDID;  //ID
    uint8_t IDE;
    uint8_t RTR;     //Request frame ou data frame
    uint8_t DLC;     //Nombre d'octets de données utiles
    uint8_t data[8]; //Tableau de 8 octets de données
}CAN_frame;

can.c

#include "can.h"

main.c

#include "can.h"
CAN_frame CAN_RxMessage;

void reception_CAN(void)
{
    //CAN_RxMessage filled with data
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it helped me! I use also extern to share global variables between files.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.