[chbot] AVR linker issue (?)

Mark Atherton markaren1 at xtra.co.nz
Sat Jul 17 22:02:31 BST 2021

I need another pair of eyes to look at this issue as am obviously doing 
something daft. This is part of a larger problem; minimal code to show 
issue is below.

All I am trying to do is move a table into a separate module, but the 
linker is giving me a hard time.

Remove 'const' in the external module and everything works fine.

Thoughts please !



mkdir -p .build
avr-g++ -c -g -Os -Wall -w -ffunction-sections -fdata-sections 
-mmcu=atmega328p -DF_CPU=16000000L font.c -o .build/font.o
avr-g++ -c -g -Os -Wall -w -ffunction-sections -fdata-sections 
-mmcu=atmega328p -DF_CPU=16000000L main.c -o .build/main.o
avr-gcc .build/font.o .build/main.o -o .build/hw.elf -Os 
-Wl,--gc-sections -mmcu=atmega328p -lm

.build/main.o: In function `main':
C:\Temp\linker_test/main.c:18: undefined reference to `ext_font'
collect2.exe: error: ld returned 1 exit status
make: *** [.build/hw.elf] Error 1

------------- font.c

#include <avr\pgmspace.h>

const unsigned char ext_font[] =        // links OK with const removed
   0x00, 0x00, 0x00, 0x00, 0x00

------------- font.h

#ifndef	_FONT_H_
#define _FONT_H_

extern const unsigned char ext_font[5]; // links OK with const removed


------------- main.c

#include <avr\io.h>
#include <avr\pgmspace.h>
#include <ctype.h>
#include "font.h"

const unsigned char local_font[] = // const has no effect on linker !!
   0x00, 0x00, 0x00, 0x00, 0x00

int main(void)
   volatile unsigned char val;
   val = local_font[3];		// links OK
   val = ext_font[3];		// link Fails


More information about the Chchrobotics mailing list