C包括文件行为

时间:2016-11-11 15:46:14

标签: c eclipse

我正在为UART库编写一些代码。我正在使用FIFO队列来缓冲IO,我有两个模块FIFO.c / h和UART.c / h。 为了与平台的HAL软件进行交互,我定义了一些我需要的结构类型,但是我在Eclipse中遇到了一些问题:

//FIFO.h

#ifndef FIFO_H
#define FIFO_H

typedef struct FIFO *FIFO;
typedef int item;

FIFO FIFO_new(int size);

void FIFO_init(FIFO f,item *buffer,int size);

int FIFO_is_empty(FIFO f);

int FIFO_is_full(FIFO f);

int FIFO_put(FIFO f,item i);

int FIFO_get(FIFO f,item *i);

void FIFO_make_empty(FIFO f);

#endif /* FIFO_H */

// UART.h

#ifndef UART_H
#define UART_H

#include "stm32f4xx_hal.h"

typedef struct
{
    UART_HandleTypeDef hal_handle;
    FIFO FIFO_buffer;                 // compiler complains
} UART_MyHandleTypeDef;

void UART_Init(UART_MyHandleTypeDef *uart,int buffer_size);

void UART_Transmit(UART_MyHandleTypeDef *uart,int datacompile);

int UART_receive(UART_MyHandleTypeDef *uart);

void UART_write(UART_MyHandleTypeDef *uart,const char *string);

#endif /* UART_H */

类型FIFO是指向在FIFO.c中定义的结构的指针(它是封装的,我不想将它暴露给客户端代码)。

我将FIFO.h中的UART.h包含在源文件中,因此UART.h应该在FIFO.h中看到typedef。但是编译器并不满意。我正在使用Eclipse进行C / C ++。

编辑:我从编译器得到的错误:

In file included from ../src/main.c:2:0:
C:/Users/l1u9c/Documents/SW4STM32/Nucleo 64 UART/inc/UART.h:9:2: error: unknown type name 'FIFO'
  FIFO FIFO_buffer;
  ^
In file included from ../src/stm32f4xx_it.c:3:0:
C:/Users/l1u9c/Documents/SW4STM32/Nucleo 64 UART/inc/UART.h:9:2: error: unknown type name 'FIFO'
  FIFO FIFO_buffer;
  ^

编辑:我在其中包含标题的源文件:

#include "FIFO.h"
#include "UART.h"
#include "stm32f4xx_hal.h"

void UART_Init(UART_MyHandleTypeDef *uart,int buffer_size)
{
    /* intialization code in application has to fill in the hal handle fields */
    HAL_UART_Init(&uart->hal_handle);
    uart->FIFO_buffer = FIFO_new(buffer_size);
}

void UART_Transmit(UART_MyHandleTypeDef *uart,int data)
{
    while (FIFO_put(uart->FIFO_buffer,data) != 0)
        ;
}

int UART_Receive(UART_MyHandleTypeDef *uart)
{
    int data;
    while (FIFO_get(uart->FIFO_buffer,&data) != 0)
        ;
    return data;
}

void UART_write(UART_MyHandleTypeDef *uart,const char *string)
{
    while (*string != '\0')
        UART_Transmit(uart,*string++);
}

void UART_IRQHandler(UART_MyHandleTypeDef *uart)
{
    if (__HAL_UART_GET_IT_SOURCE(&uart->hal_handle,UART_IT_TXE))
    {

    }
    if (__HAL_UART_GET_IT_SOURCE(&uart->hal_handle,UART_IT_RXNE))
    {

    }
}

1 个答案:

答案 0 :(得分:0)

  

我将FIFO.h中的UART.h包含在源文件中,因此UART.h应该在FIFO.h中看到typedef。

这不是包含文件的工作原理。如果您的源文件包含:

a.h

然后此源文件将按此顺序查看b.hfifo.h的定义 - 但是头文件本身不受影响。

拥有依赖于包含文件顺序的代码通常也被认为是一个坏主意。有时不幸的是,这是不可避免的,但这种情况应该尽量减少并清楚地解释。在您的情况下,您应该只在uart.h中加入fifo.h,并且您的FIFO结构应该在fifo.c而不是.c中定义。封装很好,但是如果你有一个多个模块所需的结构,它不应该隐藏在 /** * @var string * * @ORM\Column(name="serial", type="integer", unique=true) */ private $serial; 文件中。