<div dir="ltr">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!<div><br></div><div>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.</div><div>So when you are compiling the font.c file with:</div><div>avr-g++ -c -g -Os -Wall -w -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L font.c -o .build/font.o</div><div><br></div><div>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.</div><div><br></div><div>Three ways around this:</div><div>Use avr-gcc so it is compiled as C which doesn't have the same compilation unit linkage rules as C++.</div><div>Include the extern declaration with the definition, so font.c looks like.</div><div>#include <avr\pgmspace.h><br><br>extern const unsigned char ext_font[] = <br>{<br>   0x00, 0x00, 0x00, 0x00, 0x00<br>};<br></div><div><br></div><div>Or include font.h into font.c so it sees the extern declaration.</div><div><br></div><div>Bry</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Jul 18, 2021 at 5:03 PM Mark Atherton <<a href="mailto:markaren1@xtra.co.nz">markaren1@xtra.co.nz</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Thanks everyone.<br>
<br>
font.h already includes the extern ext, see below.<br>
<br>
Col, thanks for taking the trouble, it is almost certainly a compiler <br>
problem.<br>
<br>
I have been poking around in the .O files using avr-objdump and font.o <br>
appears to be missing ext_font whereas it is in one of the .text. <br>
sections associated with main.c.<br>
<br>
I also updated the sample to use a volatile index to fetch data from <br>
each array (just in case the optimiser got clever and replaced each <br>
array with a single byte read); no change.<br>
<br>
I moved both arrays into PROGMEM, and attempted to read using <br>
pgm_read_byte(). No change in reported error, and still no sign of an <br>
array in font.o.<br>
<br>
Loaded both arrays with non-zero values just in case the compiler was <br>
trying to get clever, and return 0 for any indexed read.<br>
<br>
Finally changed the arrays to have different content, just in case the <br>
compiler attempted to share identical arrays.<br>
<br>
That was a couple of hours fun, and I don't want to be right, I just <br>
want to be happy (who said that ???).<br>
<br>
Anyway, taken the B plan and dropped the font file directly into the LCD <br>
driver module, added a simple character interface and all done.<br>
<br>
Now know a lot more than I would have liked about this particular <br>
toolchain (which includes the ATmega328PB) and the SSD1306 based OLED <br>
I2C LCD displays (only need text, and refuse to use the Arduino IDE).<br>
<br>
Regards,<br>
<br>
-Mark<br>
<br>
<br>
<br>
On 18/07/2021 3:32 PM, Bry Ashman wrote:<br>
> This is because C++ has internal linkage by default for const.<br>
> <br>
> When building font.c with the c++ compiler it never only sees "const <br>
> char ..." which only has internal linkage, so it is not made available <br>
> outside of its translation unit. If that is changed to "extern const <br>
> char ext_font[] = {..};" the compiler will make it available for <br>
> external linkage and the linker will be able to resolve the symbol. (Or <br>
> if you include font.h into font.c so it sees the extern declaration)<br>
> <br>
> The extern keyword in C++ has a few more modes than it does in C... and <br>
> translation unit visibility is a bit different.<br>
> <br>
> Bry<br>
> <br>
> <br>
> <br>
> On Sun, Jul 18, 2021 at 9:02 AM Mark Atherton <<a href="mailto:markaren1@xtra.co.nz" target="_blank">markaren1@xtra.co.nz</a> <br>
> <mailto:<a href="mailto:markaren1@xtra.co.nz" target="_blank">markaren1@xtra.co.nz</a>>> wrote:<br>
> <br>
>     I need another pair of eyes to look at this issue as am obviously doing<br>
>     something daft. This is part of a larger problem; minimal code to show<br>
>     issue is below.<br>
> <br>
>     All I am trying to do is move a table into a separate module, but the<br>
>     linker is giving me a hard time.<br>
> <br>
>     Remove 'const' in the external module and everything works fine.<br>
> <br>
>     Thoughts please !<br>
> <br>
>     -Mark<br>
> <br>
> <br>
>     -------------<br>
> <br>
>     mkdir -p .build<br>
>     avr-g++ -c -g -Os -Wall -w -ffunction-sections -fdata-sections<br>
>     -mmcu=atmega328p -DF_CPU=16000000L font.c -o .build/font.o<br>
>     avr-g++ -c -g -Os -Wall -w -ffunction-sections -fdata-sections<br>
>     -mmcu=atmega328p -DF_CPU=16000000L main.c -o .build/main.o<br>
>     avr-gcc .build/font.o .build/main.o -o .build/hw.elf -Os<br>
>     -Wl,--gc-sections -mmcu=atmega328p -lm<br>
> <br>
>     .build/main.o: In function `main':<br>
>     C:\Temp\linker_test/main.c:18: undefined reference to `ext_font'<br>
>     collect2.exe: error: ld returned 1 exit status<br>
>     make: *** [.build/hw.elf] Error 1<br>
> <br>
>     ------------- font.c<br>
> <br>
>     #include <avr\pgmspace.h><br>
> <br>
>     const unsigned char ext_font[] =        // links OK with const removed<br>
>     {<br>
>         0x00, 0x00, 0x00, 0x00, 0x00<br>
>     };<br>
> <br>
>     ------------- font.h<br>
> <br>
>     #ifndef _FONT_H_<br>
>     #define _FONT_H_<br>
> <br>
>     extern const unsigned char ext_font[5]; // links OK with const removed<br>
> <br>
>     #endif<br>
> <br>
>     ------------- main.c<br>
> <br>
>     #include <avr\io.h><br>
>     #include <avr\pgmspace.h><br>
>     #include <ctype.h><br>
>     #include "font.h"<br>
> <br>
>     const unsigned char local_font[] = // const has no effect on linker !!<br>
>     {<br>
>         0x00, 0x00, 0x00, 0x00, 0x00<br>
>     };<br>
> <br>
>     int main(void)<br>
>     {<br>
>         volatile unsigned char val;<br>
>         val = local_font[3];         // links OK<br>
>         val = ext_font[3];           // link Fails<br>
> <br>
>     }<br>
> <br>
>     _______________________________________________<br>
>     Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
>     <mailto:<a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a>><br>
>     <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
>     Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
>     Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for<br>
>     venue, directions and dates.<br>
>     When replying, please edit your Subject line to reflect new subjects.<br>
> <br>
> <br>
> _______________________________________________<br>
> Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
> <a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
> Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
> Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
> When replying, please edit your Subject line to reflect new subjects.<br>
> <br>
<br>
<br>
_______________________________________________<br>
Chchrobotics mailing list <a href="mailto:Chchrobotics@lists.ourshack.com" target="_blank">Chchrobotics@lists.ourshack.com</a><br>
<a href="https://lists.ourshack.com/mailman/listinfo/chchrobotics" rel="noreferrer" target="_blank">https://lists.ourshack.com/mailman/listinfo/chchrobotics</a><br>
Mail Archives: <a href="http://lists.ourshack.com/pipermail/chchrobotics/" rel="noreferrer" target="_blank">http://lists.ourshack.com/pipermail/chchrobotics/</a><br>
Meetings usually 3rd Monday each month. See <a href="http://kiwibots.org" rel="noreferrer" target="_blank">http://kiwibots.org</a> for venue, directions and dates.<br>
When replying, please edit your Subject line to reflect new subjects.</blockquote></div>