Search code examples
embeddedkeilcortex-mlpc

How to fix 'Multiply defined' in Keil V5


I am trying to compile my code but I get the error "multiply defined" despite defining my variable only in one header (For example ".\Objects\LCDADC.axf: Error: L6200E: Symbol Pin_D6 multiply defined (by lcd.o and main.o).".)

I am using Keil with an LPC1768

main.c

#include <lpc17xx.h>
#include "LCD.h"
#include "Delay.h"


//Char LCD Pins
#define LCD_RS P2_0
#define LCD_RW P2_1
#define LCD_E P2_2
#define LCD_D4 P2_4
#define LCD_D5 P2_5
#define LCD_D6 P2_6
#define LCD_D7 P2_7

int main(){
    SystemInit();
    Delay_init();
    LCD_Init(LCD_RS, LCD_RW, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);

    int main....

LCD.H

#include "Delay.h"

uint8_t Pin_RS;
uint8_t Pin_RW;
uint8_t Pin_E;
uint8_t Pin_D4;
uint8_t Pin_D5;
uint8_t Pin_D6;
uint8_t Pin_D7;

void LCD_Init(uint8_t rs, uint8_t rw, uint8_t e, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7);

....(More functions)

LCD.c

#include "LCD.h"
#include "GPIO.h"
#include "Delay.h"

void LCD_Init(uint8_t rs, uint8_t rw, uint8_t e, uint8_t d4, uint8_t d5, uint8_t d6, uint8_t d7)
{
    //Set Pin Numbers
    Pin_RW = rw;
    Pin_E = e;
    Pin_RS = rs;
    Pin_D4 = d4;
    Pin_D5 = d5;
    Pin_D6 = d6;
    Pin_D7 = d7;

    //Set port Directions
    GPIO_PinDirection(Pin_D4, 1);
    ....(same for every pin and some command sending.)

}

....(Other Functions.)

(Sorry for posting my entire code but I believe it's important and very short in this case.)

As you can see I clearly only defined my pins only once. so why does it think I'm defining it multiple times?


Solution

  • You have declared those variables in header file LCD.h. Whenever you include the header file, those variables will be declared.

    You have included that file in main.c and in LCD.c which means two instances of each variable are created. As these variables are Global, you cannot have same names twice. That's why you are getting the error.

    To solve that, move those variables in LCD.c. If you are not going to use them outside this C file, make them Static. That way, they are restricted to LCD.c only.

    One more tip (which is unrelated to the error) is you should use Include Guards. Your Delay.h is getting included multiple times.