Use typedef struct in another file in C

1.4k views Asked by At

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).

1

There are 1 answers

1
Thien On BEST ANSWER

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
}