Electrical – Keil : LPC1768 UART : Error: L6200E: Symbol a multiply defined (by main.o and uart.o)

armkeillpc

I am learning ARM programming. I've configured uart of lpc2148 before. But there I included entire code in single file. Today I separated them in .c and .h file. But When tried to build, I got following error

uart.axf: Error: L6200E: Symbol a multiply defined (by main.o and uart.o).
uart.axf: Error: L6200E: Symbol i multiply defined (by main.o and uart.o).
Target not created

Below is the code
main.c

//main.c

#include "uart.h"

int main(void)  {

    uart_init();
    uart_tx("Hello\r\n");

    while(1)
    {
        uart_rx();
        uart_tx(a);
        uart_tx("\r\n");
    }
    return 0;
}

uart.c

//uart.c
#include "uart.h"

void uart_init()    {
    PINSEL0 |= (5<<0);
    U0LCR = 0X83;
    U0DLL = 97;
    U0LCR = 0X03;

}

void uart_tx(char *data)    {
    while(*data)    {
        U0THR = *data;
        while(!(U0LSR&(1<<5)));
        data++;
    }
}

void uart_rx()  {
    for(i = 0; ; i++)   {
        while(!(U0LSR&(1<<0)));
        a[i] = U0RBR;

        if(a[i] == '\r')    {
            a[i] = '\0';
            break;
        }
    }
}

uart.h

//uart.h

#ifndef UART_H
#define UART_H

#include <LPC214X.H>
#include <stdio.h>
#include <stdint.h>

char a[100];
int i;

void uart_init();
void uart_tx(char *);
void uart_rx();

#endif

I found some links, but none of it seems to give a solution

link1

link2

Best Answer

You have declared a and i in a header, so they're being defined in each translation unit they're compiled in. You should put them in their respective implementation files, static if they don't need to be shared and in the smallest scope you can.

i for example is used only as a loop counter in the rx function, either declare it at the top of the function or if you're using c99 then declare it in the loop.