[chbot] AVR linker issue (?)

Bry Ashman bryashman at gmail.com
Sun Jul 18 04:32:38 BST 2021


This is because C++ has internal linkage by default for const.

When building font.c with the c++ compiler it never only sees "const char
..." which only has internal linkage, so it is not made available outside
of its translation unit. If that is changed to "extern const char
ext_font[] = {..};" the compiler will make it available for external
linkage and the linker will be able to resolve the symbol. (Or if you
include font.h into font.c so it sees the extern declaration)

The extern keyword in C++ has a few more modes than it does in C... and
translation unit visibility is a bit different.

Bry



On Sun, Jul 18, 2021 at 9:02 AM Mark Atherton <markaren1 at xtra.co.nz> wrote:

> 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 !
>
> -Mark
>
>
> -------------
>
> 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
>
> #endif
>
> ------------- 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
>
> }
>
> _______________________________________________
> Chchrobotics mailing list Chchrobotics at lists.ourshack.com
> https://lists.ourshack.com/mailman/listinfo/chchrobotics
> Mail Archives: http://lists.ourshack.com/pipermail/chchrobotics/
> Meetings usually 3rd Monday each month. See http://kiwibots.org for
> venue, directions and dates.
> When replying, please edit your Subject line to reflect new subjects.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ourshack.com/pipermail/chchrobotics/attachments/20210718/864fb6f6/attachment-0001.html>


More information about the Chchrobotics mailing list