# Problem with Linker including same binary structure twice

**URL:** <https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196>\
**Category:** Programming\
**Created:** [January 9, 2022, 8:44pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196 "2022-01-09T20:44:19Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![lorandil](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lorandil](https://forum.arduino.cc/u/lorandil)\
**Post date:** [January 9, 2022, 8:44pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/1 "2022-01-09T20:44:19Z")

</div>

Hi folks!

I am working on a project for the ATtiny85, so flash memory is always a problem.

I have some bitmaps defined in a separate header file, defined like this:

_const unsigned char joey [] PROGMEM = { 0x00, 0x00, 0x00, 0x00, ........, 0xf0, 0x98, 0x9c };_

Then I have a table to access all these bitmaps by a key value (defined in the same header file):  
_// list of possible non wall objects (i.e. monsters, doors, ...) (11 bytes per object)  
const NON\_WALL\_OBJECT objectList [] PROGMEM = {  
// itemType , width, verticalOffsetBits, heightBits, lineOffset, maxView, scalingThreshold, bitmapData  
{ SKELETON , 28, 2 \* 8, 5 \* 8, 56, 3, { 1, 2, 99 }, joey },  
...  
{ RAT , 20, 5 \* 8, 2 \* 8, 40, 2, { 1, 2, 99 }, rat },  
};_

My problem occurs if I'm accessing this table (or a single bitmap, e.g. _joey_) in two methods of a class (e.g. _Dungeon_).  
This appears to make the linker include the bitmaps (if a bitmap _joey_ was accessed)  
or all bitmaps in the table (when _objectList_ was accessed) a second time.  
I'm quite sure, because my flash size increases by _sizeof(joey)_ or _sizeof(all bitmaps)_.

I don't understand this behaviour...  
I'm sure that all bitmaps are already in the flash, because I'm using them all the time.  
The code is working fine on a larger MCU and compiles without warnings.

I can reproduce the problem with Arduino 1.8.16 (avr-gcc) and Arduino 2.0.0rc3 (clang), so this will most probably be a problem with my code.

Any ideas? I can provide more code if necessary.

Thanks in advance,  
Sven

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [January 9, 2022, 8:48pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/2 "2022-01-09T20:48:58Z")

</div>

> [@lorandil](#):
>
> I can provide more code if necessary.

Please post a small but complete sketch that illustrates the problem

---

<div class="post-metadata">

**Author:** ![lorandil](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lorandil](https://forum.arduino.cc/u/lorandil)\
**Post date:** [January 10, 2022, 9:34pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/4 "2022-01-10T21:34:07Z")

</div>

Ok, I created a minimal sketch and I think I found the source of the problem:

My project consists of

- a project file (something.ino)
- a header file containing all the bitmaps (e.g. 'joey')
- a class header file (myClass.h)
- TWO files containing the class member functions (myClassA.cpp, myClassB.cpp),  
because I wanted to split the methods logically.

If methods in both class implementation files access 'joey', 'joey' will be linked twice.

Is there anything I can do against this except merging myClassA.cpp and myClassB.cpp  
back into one file?

Shouldn't the linker "know" that it is the same resource? ('#pragma once') is set...

Please enlighten me 🙂

Sven

---

<div class="post-metadata">

**Author:** ![lorandil](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lorandil](https://forum.arduino.cc/u/lorandil)\
**Post date:** [January 11, 2022, 9:07pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/5 "2022-01-11T21:07:22Z")

</div>

Hi folks, perhaps somebody is still watching?

Here is the a full sample sketch:

[bitmapDrawing.cpp](https://forum.arduino.cc/uploads/short-url/ltZeS1dJPLukLp7zYlDJp4fFSlR.cpp) (499 Bytes)  
[dungeon.cpp](https://forum.arduino.cc/uploads/short-url/5sscUXqY1TYdveIMd1pt0qWPOns.cpp) (464 Bytes)  
[dungeon.h](https://forum.arduino.cc/uploads/short-url/cz1tJbgr1GHt5m0ByXlEWT50rp5.h) (761 Bytes)  
[Problem.ino](https://forum.arduino.cc/uploads/short-url/wuiABAcbWwO3i3CxclUZfwWyUte.ino) (101 Bytes)

With the ATTinyCore the sketch uses 4218 bytes when the bitmap is only linked once.  
If 9 line in **bitmapDrawing.cpp** is uncommented,  
the sketch size increases by 4096 bytes, blowing the flash of the ATtiny85.

I'm still hoping for an explanation.

Sven

---

<div class="post-metadata">

**Author:** ![in0](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@in0](https://forum.arduino.cc/u/in0)\
**Post date:** [January 11, 2022, 9:49pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/6 "2022-01-11T21:49:58Z")

</div>

> [@lorandil](#):
>
> If 9 line in **bitmapDrawing.cpp** is uncommented,

Did you mean line 9 of `dungeon.cpp`?

```cpp
#define DISPLAY_SCALED_BITMAP

```

* * *

> [@lorandil](#):
>
> Shouldn't the linker "know" that it is the same resource? ('#pragma once') is set...

I don't know anything about the first part, but `#pragma once` only prevents double inclusion within a given translation unit. I think the more standard alternative ["include guard"](https://en.wikipedia.org/wiki/Include_guard) approach makes this more clear.

The `.ino` files of the sketch, `bitmapDrawing.cpp`, and `dungeon.cpp` are each separate translation units, so you get a full copy of `dungeon.h` in each of them.

---

<div class="post-metadata">

**Author:** ![lorandil](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lorandil](https://forum.arduino.cc/u/lorandil)\
**Post date:** [January 12, 2022, 8:09pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/7 "2022-01-12T20:09:02Z")

</div>

> [@in0](#):
>
> Did you mean line 9 of `dungeon.cpp`?
> 
> ``

Yes, I did - sorry for the confusion.

Ok, that explains the identical behaviour of the two compilers and confirms my guess that the problem **is** in my code... 🙂

I will try to declare the structure **extern** in one of the modules and  
give it another try. Of course I could stick with all methods in one file, but that suck's.

Thanks for all replies,  
Sven

---

<div class="post-metadata">

**Author:** ![lorandil](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lorandil](https://forum.arduino.cc/u/lorandil)\
**Post date:** [January 14, 2022, 8:53pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/8 "2022-01-14T20:53:24Z")

</div>

I tried with **extern** and get my code to compile, but the linker errors out because it doesn't find the structure I declated **extern**.  
I give up for now, the one file approach works good enough and due to the limited flash capacity the files can't get too large 😜

_I will mark in0's reply as a solution, as it explains the root cause for the behaviour._

If anyone has a solution how to get it working with multiple source files, please let me know any 😉

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 14, 2022, 10:06pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/9 "2022-01-14T22:06:37Z")

</div>

> [@lorandil](#):
>
> if anyone has a solution how to get it working with multiple source files, please let me know any

See my [Post #5 in this thread](https://forum.arduino.cc/t/organising-code-for-a-large-scale-modular-project/591328/5).

---

<div class="post-metadata">

**Author:** ![lorandil](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lorandil](https://forum.arduino.cc/u/lorandil)\
**Post date:** [January 16, 2022, 2:04pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/10 "2022-01-16T14:04:20Z")

</div>

@gfvalvo Thanks for your tips on "best practices". They are surely true!

Nonetheless that doesn't answer my remaining question on how to share access to a structure from two source files (implementing methods of the same class):

> [@lorandil](#):
>
> If anyone has a solution how to get it working with multiple source files, please let me know

Stay safe,  
Sven

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [January 16, 2022, 2:08pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/11 "2022-01-16T14:08:58Z")

</div>

> [@lorandil](#):
>
> Nonetheless that doesn't answer my remaining question on how to share access to a structure from two source files (implementing methods of the same class):

Post an [MRE](https://stackoverflow.com/help/minimal-reproducible-example) that demonstrates the problem. Make sure it's the smallest amount of code and files that demonstrates the problem (and only that problem) without any extraneous clutter. Post it in-line with code tags not as files I must download.

---

<div class="post-metadata">

**Author:** ![MicroBahner](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/microbahner/32/238923_2.png) [@MicroBahner](https://forum.arduino.cc/u/MicroBahner)\
**Post date:** [January 16, 2022, 2:17pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/12 "2022-01-16T14:17:51Z")

</div>

> [@lorandil](#):
>
> but the linker errors out because it doesn't find the structure I declated **extern**.

You must **define** this structure in **one** of your .cpp files.  
.h files should only contain the declarations ( extern ... ), while you must have **only one** definition in a .cpp file.

@gfvalvo described this very well in his link.

---

<div class="post-metadata">

**Author:** ![lorandil](https://avatars.discourse-cdn.com/v4/letter/l/ecd19e/32.png) [@lorandil](https://forum.arduino.cc/u/lorandil)\
**Post date:** [January 16, 2022, 4:18pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/13 "2022-01-16T16:18:33Z")

</div>

Mea Culpa - I seem to have missed something the last time I tried to proceed the way @gfvalvo suggested in his linked article.  
When I tried to create an MRE, everything worked as it was supposed to - so no more problem here anymore.  
Just a simple **UTSE** (User too stupid error) 😉

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [July 15, 2022, 4:19pm UTC](https://forum.arduino.cc/t/problem-with-linker-including-same-binary-structure-twice/945196/14 "2022-07-15T16:19:31Z")

</div>

Closed on July 15, 2022, 4:19pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
