多重定义 - GCC虚假错误 [英] Multiple definitions of - GCC bogus error

查看:907
本文介绍了多重定义 - GCC虚假错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个头文件 lcd.h用于与此(缩短):

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...

和文件可见的lcd.c ,其中该变量用于:

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));
}

我包括 lcd.h用于的main.c 可见的lcd.c 正在使用的makefile单独编译。

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

我得到这个错误:

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

我已经检查了文件,并多次出现的变量没有其他定义。 第一次这里定义是一个谎言,在的main.c 行包含一个无限循环,code,没有什么与此有关。

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.

这是怎么回事?

(PS如果我移动变量到可见的lcd.c 文件,它编译好的 - 但我想有它的头文件,所以可以从其他地方访问,太)

(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)

推荐答案

LD是正确的。您在感谢两个独立的编译单元定义为标题中的同一个变量。

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

解决这个问题的办法是把它声明的extern 在头,并只的一个单位的重新声明它没有的extern 预选赛中的.c文件。

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.

这篇关于多重定义 - GCC虚假错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆