0

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?

dbush
  • 205,898
  • 23
  • 218
  • 273
Tinchu
  • 89
  • 1
  • 6
  • You should make sure your own headers are both self-contained and idempotent. Most systems headers are like that too, but if you come across one that isn't, it is a pain. The header guards make them idempotent (including them more than once is the same as including them once). Making sure that a header is self-contained is easy too — sort of. You ensure that a module can be compiled with the header as the first included file. There multiple questions about this, including [Should I use `#include` in headers?](http://stackoverflow.com/questions/1804486/). – Jonathan Leffler Oct 22 '16 at 00:42
  • Note too that if the headers `port_pin.h`, `hal_io.h`, `drv_io.h` and `board_cfg.h` are yours, you should not be using the `__HDRNAME_H__` include guards. Names starting with two underscores are reserved for the implementation to use. OTOH, if they're provided by the implementation, then they're completely correct; you can't use those names (legitimately) so they can't affect your program in untoward ways. – Jonathan Leffler Oct 22 '16 at 00:45

2 Answers2

3

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.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • In file drv_io.c I am including board_cfg.h before drv_io.h – Tinchu Oct 21 '16 at 15:30
  • Why it is giving error in drv_io.h. Complier will include the .h file and then do the compilation. Why it is givng error then. – Tinchu Oct 21 '16 at 15:32
  • Status_t is defined in common.h. it was a big file so i havent included it here. My point is in drv_io.h if i exculde the argument ... BoardCfgPortPin_t * ... Then it is showing no error at all. Conceptually i am including all the declerations still getting error. – Tinchu Oct 21 '16 at 16:25
  • @user1093152 The problem is that your includes are out of order. See my edit. – dbush Oct 21 '16 at 18:10
1

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
Karsten Koop
  • 2,475
  • 1
  • 18
  • 23