我在头文件中有以下代码:
#ifndef BUFFER_H
#define BUFFER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct c_buff
{
void *buffer; // data buffer
void *buffer_end; // end of data buffer
size_t capacity; // maximum number of items in the buffer
size_t count; // number of items in the buffer
size_t sz; // size of each item in the buffer
void *head; // pointer to head
void *tail; // pointer to tail
};
void cb_init(c_buff *cb, size_t capacity, size_t sz)
{
cb->buffer = malloc(capacity * sz);
if(cb->buffer == NULL) {
// handle error
}
cb->buffer_end = (char *)cb->buffer + capacity * sz;
cb->capacity = capacity;
cb->count = 0;
cb->sz = sz;
cb->head = cb->buffer;
cb->tail = cb->buffer;
}
#endif
以下c文件
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <common.h>
#include <usart.h>
#include <buffer.h>
struct c_buff usart_buffer;
struct c_buff *usart_buffer_ptr;
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
void initUSART(void) {
SETBIT(UCSR0A, UDRE0);
//SETBIT(UCSR0A, U2X0);
SETBIT(UCSR0C, UCSZ01);
SETBIT(UCSR0C, UCSZ00);
UBRR0 = 25;
SETBIT(UCSR0B, RXCIE0);
SETBIT(UCSR0B, TXCIE0);
SETBIT(UCSR0B, RXEN0);
SETBIT(UCSR0B, TXEN0);
}
ISR(USART_RX_vect) {
char data;
data = UDR0;
UDR0 = data;
}
ISR(USART_TX_vect) {
}
当我尝试编译这个时,我得到一个指向这一行的错误:
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
它只是在数字常量之前说“错误:预期'”'。
谷歌告诉我这是某种预处理器错误。但我不明白这是怎么回事。我是C的新手,所以如果这是非常明显的话,我会道歉。
答案 0 :(得分:4)
您无法在顶层进行裸体功能调用。
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
是一个裸函数调用。在main()
内移动。
答案 1 :(得分:3)
您无法在全局范围内运行功能。它必须在main中完成:
int main(int argc, char *argv[] {
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
}
答案 2 :(得分:2)
问题是您正在尝试在文件级别执行方法。
cb_init(usart_buffer_ptr, USART_BUFFER_SIZE, sizeof(char));
C语言只允许此级别的声明/定义而不是实际执行的语句。需要将此调用移动到函数定义中。