Multiple definitions of - GCC bogus error

走远了吗. 提交于 2019-12-11 11:26:22

问题


I have a header file lcd.h with this (shortened):

#pragma once
// ...
const uint8_t LCD_ROW_ADDR[] = {0x00, 0x40, 0x14, 0x54};
// ... other prototypes and macros...

And a file lcd.c where this variable is used:

#include <stdbool.h>
#include <stdint.h>
// ...

#include "lcd.h"

// ...

/** Set cursor position */
void lcd_xy(const uint8_t x, const uint8_t y)
{
    lcd_set_addr(LCD_ROW_ADDR[y] + (x));
}

I include lcd.h in main.c, and lcd.c is compiled separately using makefile.

I'm getting this error: "multiple definition of `LCD_ROW_ADDR'"

avr-gcc -std=gnu99 -mmcu=atmega328p -DF_CPU=16000000UL -I. -Ilib/ -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wall -Wno-main -Wno-strict-prototypes -Wno-comment -g2 -Wextra -Wfatal-errors -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax -DDEBO_CHANNELS=6 -DDEBO_TICKS=1 -Os main.c lib/debounce.c lib/lcd.c lib/adc.c  --output main.elf
/tmp/ccgfa1iK.o: In function `lcd_xy':
/home/ondra/git/avr-projects/devel/lcdsnake/lib/lcd.c:203: multiple definition of `LCD_ROW_ADDR'
/tmp/cc0iT39O.o:/home/ondra/git/avr-projects/devel/lcdsnake/main.c:424: first defined here
/usr/bin/avr-ld: Disabling relaxation: it will not work with multiple definitions
collect2: error: ld returned 1 exit status
Makefile:87: recipe for target 'main.elf' failed
make: *** [main.elf] Error 1

I have checked the files multiple times and there is no other definition of the variable. The "first defined here" is a lie, the line in main.c contains an infinite loop code, nothing related to this.

What's going on here?

(ps. If I move the variable into the lcd.c file, it compiles OK - but I want to have it in the header file, so it can be accessed from elsewhere, too)


回答1:


ld is correct. You have the same variable defined in two separate compilation units thanks to the header.

The way to fix this is to declare it extern in the header, and have only one of the units redeclare it without the extern qualifier in its .c file.



来源:https://stackoverflow.com/questions/29189045/multiple-definitions-of-gcc-bogus-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!