[chbot] AVR linker issue (?)

Bry Ashman bryashman at gmail.com
Sun Jul 18 06:50:16 BST 2021


Wasn't quite clear enough I think and made some typos... I was trying to
write that response as I was running out the door!

font.h isn't where the extern matters in this case from the font.o
compilation unit point of view since it isn't included in font.c.
So when you are compiling the font.c file with:
avr-g++ -c -g -Os -Wall -w -ffunction-sections -fdata-sections
-mmcu=atmega328p -DF_CPU=16000000L font.c -o .build/font.o

All it ever sees is: const unsigned char ext_font[] ... and since you are
compiling in C++ land, not C. That is just a constant with internal
linkage. So as far as the compiler is concerned it is only valid to be used
in that file and is free to optimise it as such.

Three ways around this:
Use avr-gcc so it is compiled as C which doesn't have the same compilation
unit linkage rules as C++.
Include the extern declaration with the definition, so font.c looks like.
#include <avr\pgmspace.h>

extern const unsigned char ext_font[] =
{
   0x00, 0x00, 0x00, 0x00, 0x00
};

Or include font.h into font.c so it sees the extern declaration.

Bry

On Sun, Jul 18, 2021 at 5:03 PM Mark Atherton <markaren1 at xtra.co.nz> wrote:

> Thanks everyone.
>
> font.h already includes the extern ext, see below.
>
> Col, thanks for taking the trouble, it is almost certainly a compiler
> problem.
>
> I have been poking around in the .O files using avr-objdump and font.o
> appears to be missing ext_font whereas it is in one of the .text.
> sections associated with main.c.
>
> I also updated the sample to use a volatile index to fetch data from
> each array (just in case the optimiser got clever and replaced each
> array with a single byte read); no change.
>
> I moved both arrays into PROGMEM, and attempted to read using
> pgm_read_byte(). No change in reported error, and still no sign of an
> array in font.o.
>
> Loaded both arrays with non-zero values just in case the compiler was
> trying to get clever, and return 0 for any indexed read.
>
> Finally changed the arrays to have different content, just in case the
> compiler attempted to share identical arrays.
>
> That was a couple of hours fun, and I don't want to be right, I just
> want to be happy (who said that ???).
>
> Anyway, taken the B plan and dropped the font file directly into the LCD
> driver module, added a simple character interface and all done.
>
> Now know a lot more than I would have liked about this particular
> toolchain (which includes the ATmega328PB) and the SSD1306 based OLED
> I2C LCD displays (only need text, and refuse to use the Arduino IDE).
>
> Regards,
>
> -Mark
>
>
>
> On 18/07/2021 3:32 PM, Bry Ashman wrote:
> > 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
> > <mailto: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
> >     <mailto: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.
> >
> >
> > _______________________________________________
> > 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.
> >
>
>
> _______________________________________________
> 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/5311ede8/attachment-0001.html>


More information about the Chchrobotics mailing list