Tinchu
Tinchu

Reputation: 99

error in struct decleration in header files

port_pin.h

#ifndef __PORT_PIN_H__
#define __PORT_PIN_H__

typedef enum
{
    IO_PORT_A    = ((uint16_t)0),   /* IO Port A Selected  */
    IO_PORT_B    = ((uint16_t)1),   /* IO Port B Selected  */
    IO_PORT_NONE = ((uint16_t)0xFF)  /* No IO Port Selected */
}PortName_t;

/* IO Driver GPIO Pin Numbers */
typedef enum
{
    IO_PIN_0    = ((uint16_t)0),      /* Pin 0 selected    */
    IO_PIN_1    = ((uint16_t)1),      /* Pin 1 selected    */
    IO_PIN_2    = ((uint16_t)2),      /* Pin 2 selected    */
    IO_PIN_3    = ((uint16_t)3),      /* Pin 3 selected    */
}PinNumber_t;

hal_io.h

#ifndef __HAL_IO_H__
#define __HAL_IO_H__

#ifdef  DEF_HAL_IO
#define EXTERN_HAL_IO
#else   
#define EXTERN_HAL_IO extern
#endif

EXTERN_HAL_IO void HalIo_fct(PortName_t, PinNumber_t);

#endif

drv_io.h

#ifndef __DRV_IO_H__
#define __DRV_IO_H__

#ifdef  DEF_DRV_IO
#define EXTERN_DRV_IO
#else   
#define EXTERN_DRV_IO extern
#endif

EXTERN_DRV_IO Status_t DDrvIOPinSet_fct(const BoardCfgPortPin_t *pointer);

#endif

board_cfg.h

#ifndef __BOARD_CFG_H__
#define __BOARD_CFG_H__

#ifdef  DEF_BOARD_CFG
#define EXTERN_BOARD_CFG
#else   
#define EXTERN_BOARD_CFG extern
#endif

/* IO driver Gpio Port and Pin Configuration */
typedef struct
{
    PortName_t  Name_en;    /* Specifies the IO Port module             */
                            /* This parameter can be a value of @ref    */
                            /* GpioPort_t                               */

    PinNumber_t PinNo_en;   /* Specifies the IO Pin number              */
                            /* This parameter can be a value of @ref    */
                            /* PinNumber_t                              */
}BoardCfgPortPin_t;

EXTERN_BOARD_CFG const BoardCfgPortPin_t BoardCfgPortPin_sta[4];

EXTERN_BOARD_CFG void BoardCfg_fct();

#endif

main.c

#include <stdio.h>
#include "common.h"
#include "port_pin.h"
#include "board_cfg.h"
#include "drv_io.h"

int main()
{
    BoardCfg_fct();

    printf("\n\n");

    return 0;
}

hal_io.c

#define DEF_HAL_IO

#include <stdio.h>
#include "common.h"
#include "port_pin.h"
#include "hal_io.h"

void HalIo_fct(PortName_t PortName_en, PinNumber_t PinNo_en)
{
    printf("\nIN HAL\n");
    printf("PORTNAME : %d, PIN NUMBER : %d", PortName_en, PinNo_en);
}

drv_io.c

#define DEF_DRV_IO

#include <stdio.h>
#include "common.h"
#include "port_pin.h"
#include "hal_io.h"
#include "board_cfg.h"
#include "drv_io.h"

Status_t DDrvIOPinSet_fct(const BoardCfgPortPin_t *pointer)
{
    printf("\nin DRV IO \n");
    HalIo_fct(pointer->Name_en, pointer->PinNo_en);
    return (PASS);
}

board_cfg.c

#define DEF_BOARD_CFG

#include <stdio.h>
#include "common.h"
#include "port_pin.h"
#include "drv_io.h"
#include "board_cfg.h"

const BoardCfgPortPin_t BoardCfgPortPin_sta[4] =
{
    { IO_PORT_B, IO_PIN_0 }, /* SENSE_INV_TEMP        */
    { IO_PORT_B, IO_PIN_1 }, /* SENSE_INV_AC          */
    { IO_PORT_B, IO_PIN_2 }, /* BUZZER                */
    { IO_PORT_B, IO_PIN_3 }, /* STAUS_230V_AC         */
};

void BoardCfg_fct()
{
    DDrvIOPinSet_fct(&BoardCfgPortPin_sta[0]);
    DDrvIOPinSet_fct(&BoardCfgPortPin_sta[1]);
    DDrvIOPinSet_fct(&BoardCfgPortPin_sta[2]);
    DDrvIOPinSet_fct(&BoardCfgPortPin_sta[3]);
}

Regarding the above files, when I try to compile the code I am getting the following errors:

drv_io.h(10): error C2143: syntax error : missing ')' before '*'

drv_io.h(10): error C2143: syntax error : missing '{' before '*'

drv_io.h(10): error C2059: syntax error : ')'

If I comment the code in board_cfg.h

/* IO driver Gpio Port and Pin Configuration */
typedef struct
{
    PortName_t  Name_en;    /* Specifies the IO Port module             */
                            /* This parameter can be a value of @ref    */
                            /* GpioPort_t                               */

    PinNumber_t PinNo_en;   /* Specifies the IO Pin number              */
                            /* This parameter can be a value of @ref    */
                           /* PinNumber_t                              */
}BoardCfgPortPin_t;

and add it to port_pin.h, I could compile the code successfully.

But I need to have the struct in board_cfg.h only

Why am I getting this error?

Upvotes: 0

Views: 100

Answers (2)

dbush
dbush

Reputation: 225212

In the file board_cfg.c, it has drv_io.h first followed by board_cfg.h. However, the delaration of DDrvIOPinSet_fct has an argument of type BoardCfgPortPin_t which is defined in board_cfg.h. Since drv_io.h is listed first, BoardCfgPortPin_t hasn't been declared yet. This is what is causing the error.

Your header files are dependent on each other. Rather than depending on the files that include them to put things in the right order, each header file needs to include the other headers they depend on.

  • In port_pin.h, add #include <stdint.h>
  • In hal_io.h, add #include "port_pin.h"
  • In drv_io.h, add #include "board_cfg.h" and #include "common.h"
  • In board_cfg.h add #include "port_pin.h"

By doing this, each header has everything it needs. Then it doesn't matter in which order they are included by source files.

Also, you don't need any of the defines related to extern. Function declarations are extern by default.

Upvotes: 3

Karsten Koop
Karsten Koop

Reputation: 2524

In line 10 of drv_io.h you have

EXTERN_DRV_IO Status_t DDrvIOPinSet_fct(const BoardCfgPortPin_t *pointer);

So you are using BoardCfgPortPin_t which means that the compiler has to know at this point what BoardCfgPortPin_t is. There are several ways to achieve this:

  • #include board_cfg.h in drv_io.h before line 10
  • use a forward declaration: put typedef struct BoardCfgPortPin_t; before line 10
  • make sure all .c files include board_cfg.h before drv_io.h; this is already the case for all but board_cfg.c

Upvotes: 1

Related Questions