Linker error when using unordered_map

Benjii

New member
Board: Teensy 3.6
Toolchain-version: teensy-compile/15.2.1

Sketch to reproduce:
C++:
using namespace std;

unordered_map<int, int> floormap_stepper = {{1, 1}};

void setup() {}

void loop() {}

When using an unordered_map, the Linker does not find a floor() reference:
Code:
Linking everything together...
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/arm-none-eabi-gcc -O2 -Wl,--gc-sections,--relax,--no-warn-rwx-segments,--defsym=__rtc_localtime=1784903286 -T/home/benji/.arduino15/packages/teensy/hardware/avr/1.62.0/cores/teensy3/mk66fx1m0.ld -mthumb -mcpu=cortex-m4 -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsingle-precision-constant -o /home/benji/.cache/arduino/sketches/9F9B2C8D6A15A62AB1000E144EF731B7/sketch_jul24a.ino.elf /home/benji/.cache/arduino/sketches/9F9B2C8D6A15A62AB1000E144EF731B7/sketch/sketch_jul24a.ino.cpp.o /home/benji/.cache/arduino/sketches/9F9B2C8D6A15A62AB1000E144EF731B7/core/core.a -L/home/benji/.cache/arduino/sketches/9F9B2C8D6A15A62AB1000E144EF731B7 -larm_cortexM4lf_math -lm -lstdc++
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: /home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(hashtable_c++0x.o): in function `std::__detail::_Prime_rehash_policy::_M_next_bkt(unsigned int) const':
hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0x68): undefined reference to `floor'
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: (floor): Unknown destination type (ARM/Thumb) in /home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0x68): dangerous relocation: unsupported relocation
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0xa2): undefined reference to `floor'
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: (floor): Unknown destination type (ARM/Thumb) in /home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0xa2): dangerous relocation: unsupported relocation
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: /home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(hashtable_c++0x.o): in function `std::__detail::_Prime_rehash_policy::_M_need_rehash(unsigned int, unsigned int, unsigned int) const':
hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x60): undefined reference to `floor'
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: (floor): Unknown destination type (ARM/Thumb) in /home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x60): dangerous relocation: unsupported relocation
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x96): undefined reference to `floor'
/home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/bin/ld: (floor): Unknown destination type (ARM/Thumb) in /home/benji/.arduino15/packages/teensy/tools/teensy-compile/15.2.1/arm/bin/../lib/gcc/arm-none-eabi/15.2.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x96): dangerous relocation: unsupported relocation
collect2: error: ld returned 1 exit status
exit status 1

I don't have much experience with the inner workings of the c++ compiler/linker toolchain so I had Claude walk me through it and it ended up suggesting me this working way to resolve this:

in "~/.arduino15/packages/teensy/hardware/avr/1.62.0/boards.txt"
find "teensy36.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++"
change it to "teensy36.build.flags.libs=-larm_cortexM4lf_math -Wl,--start-group -lm -lstdc++ -Wl,--end-group"

So that ld rescans them until everything resolves as (if I get it right) unordered_maps dependancy of floor() is realized at the -lstdc step but floor() would be found at the -lm step, but as the -lm step is already done, the linker does not revisit it to resolve the floor() dependancy.
With the two arguments grouped, the linker can revisit them which resolves this.

I don't know if this way to resolve this imposes other things not wanted by Teensyduino, I just wanted to resolve this issue for me and help the ones looking for this after me.

I hope that this gets fixed in the next toolchain-version as it would overwrite this change made in the boards.txt.

Feel free point me to relevant other issues if this is a duplicate of anything :D
 
@PaulStoffregen or other might investigate or clarify if that is a safe update/proper to make to the Teensy boards.txt.

Just a suggestion to try: Move that EDIT into boards.local.txt (and restore the original command) and restart the IDE and see if it picks it up as a replacement command for the line. That .local. file with still need to be copied back in with each update to the TeensyDuino build - but would not require editing the PJRC source.
 
Code:
ld.exe: C:\Program Files (x86)\Arduino\hardware\tools\arm\bin\..\lib\gcc\arm-none-eabi\15.2.1\..\..\..\..\arm-none-eabi\lib\thumb\v7e-m+fp\hard\libstdc++.a(hashtable_c++0x.o): in function std::__detail::_Prime_rehash_policy::_M_next_bkt(unsigned int) const
hashtable_c++0x.cc*: (.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0x68): undefined reference to floor
ld.exe: (floor): Unknown destination type (ARM\Thumb) in C:\Program Files (x86)\Arduino\hardware\tools\arm\bin\..\lib\gcc\arm-none-eabi\15.2.1\..\..\..\..\arm-none-eabi\lib\thumb\v7e-m+fp\hard\libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc*: (.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0x68): dangerous relocation: unsupported relocation
ld.exe: hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0xa2): undefined reference to floor
ld.exe: (floor): Unknown destination type (ARM\Thumb) in C:\Program Files (x86)\Arduino\hardware\tools\arm\bin\..\lib\gcc\arm-none-eabi\15.2.1\..\..\..\..\arm-none-eabi\lib\thumb\v7e-m+fp\hard\libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc*: (.text._ZNKSt8__detail20_Prime_rehash_policy11_M_next_bktEj+0xa2): dangerous relocation: unsupported relocation
ld.exe: C:\Program Files (x86)\Arduino\hardware\tools\arm\bin\..\lib\gcc\arm-none-eabi\15.2.1\..\..\..\..\arm-none-eabi\lib\thumb\v7e-m+fp\hard\libstdc++.a(hashtable_c++0x.o): in function std::__detail::_Prime_rehash_policy::_M_need_rehash(unsigned int, unsigned int, unsigned int) const
hashtable_c++0x.cc*: (.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x60): undefined reference to floor
ld.exe: (floor): Unknown destination type (ARM\Thumb) in C:\Program Files (x86)\Arduino\hardware\tools\arm\bin\..\lib\gcc\arm-none-eabi\15.2.1\..\..\..\..\arm-none-eabi\lib\thumb\v7e-m+fp\hard\libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc*: (.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x60): dangerous relocation: unsupported relocation
ld.exe: hashtable_c++0x.cc:(.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x96): undefined reference to floor
ld.exe: (floor): Unknown destination type (ARM\Thumb) in C:\Program Files (x86)\Arduino\hardware\tools\arm\bin\..\lib\gcc\arm-none-eabi\15.2.1\..\..\..\..\arm-none-eabi\lib\thumb\v7e-m+fp\hard\libstdc++.a(hashtable_c++0x.o)
hashtable_c++0x.cc*: (.text._ZNKSt8__detail20_Prime_rehash_policy14_M_need_rehashEjjj+0x96): dangerous relocation: unsupported relocation
collect2.exe*: error: ld returned 1 exit status
 

Error linking for board Teensy 3.6 (teensy36)
Using Arduino 1.8.19 still gives errors as shown above. This is with the latest TeensyDuino.
 
@PaulStoffregen or other might investigate or clarify if that is a safe update/proper to make to the Teensy boards.txt.

Just a suggestion to try: Move that EDIT into boards.local.txt (and restore the original command) and restart the IDE and see if it picks it up as a replacement command for the line. That .local. file with still need to be copied back in with each update to the TeensyDuino build - but would not require editing the PJRC source.
This works like you said, thank you for the suggestion (y)
 
Looking at this today, just changing the order so the math library is last seems to solve this problem.

In other words, changing this

Code:
teensy36.build.flags.libs=-larm_cortexM4lf_math -lm -lstdc++

to this

Code:
teensy36.build.flags.libs=-larm_cortexM4lf_math -lstdc++ -lm

I'm reluctant to use --start-group as the default for everyone, because the documentation advises against it unless "unavoidable circular references". I am skeptical that would be necessary with the standard libraries.

--start-group archives --end-group
The archives should be a list of archive files. They may be either explicit file names, or -l options.
The specified archives are searched repeatedly until no new undefined references are created. Normally, an archive is searched only once in the order thatit is specified on the command line. If a symbol in that archive is needed to resolve an undefined symbol referred to by an object in an archive that appearslater on the command line, the linker would not be able to resolve that reference. By grouping the archives, they all be searched repeatedly until all possiblereferences are resolved.

Using this option has a significant performance cost. It is best to use it only when there are unavoidable circular references between two or morearchives.
 
Unfortunately the above doesn’t work for me, for the issue I reported here. I have no idea why this has suddenly happened, or why changing CPU speed fixes it. It looks very like the sort of Heisenbug that will defy any effort to cut down my 12,000+ line plus 14 modified libraries / cores to a manageable example.
 
Interesting if I run this

Code:
using namespace std;

unordered_map<int, int> floormap_stepper = {{1, 1}};

void setup() {}

void loop() {}

on a Teensy 3.6 with with fast LTO or fastest LTO it compiles with no linker errors.

and if I run this
Code:
#include <Streaming.h>
#include <unordered_map>
using namespace std;
#define cout Serial
void setup() {
    cout.begin(115200);
    delay(3000);
    // Creating an unordered_map with integer
    // keys and string values
    unordered_map<int, String> um = {{1, "Geeks"}, {2, "For"}, {3, "C++"}};
    for (auto &i : um)
        cout << i.first << ": " << i.second << endl;
}
void loop() {}


It works
Code:
3: C++
2: For
1: Geeks

Note: On the Teensy 4.1 you don't get the error.
 
Last edited:
That seems to be how it goes. You work on something for 5 months and 176 commits, then it throws a completely weird and spurious link error at you, which is "cured" by changing CPU speed o_O . Or in your case, to a different optimisation.
 
It looks very like the sort of Heisenbug that will defy any effort to cut down my 12,000+ line plus 14 modified libraries / cores to a manageable example.

If you can send it to me (private email ok), I will try compiling it here. Can't make any promises, but just being able to reproduce it would be a first step.
 
Last edited:
That seems to be how it goes. You work on something for 5 months and 176 commits, then it throws a completely weird and spurious link error at you, which is "cured" by changing CPU speed o_O . Or in your case, to a different optimisation.
Does the issue then come back if you change back to the origional settings?

Changing the optimization settings will force a full recompile. Could it possibly be an issue where the build files are somehow getting messed up, only performing an incremental build then pulls in those same files every time. By forcing the full recompile you clear the issue.

A long shot I know but over the years I've seen enough issues get magically fixed by doing a clean build that it's always worth checking.
 
Thanks Paul. I tried it again this morning, and now I can't reproduce it myself! In the mean time I'd pulled the most recent changes into an M1 Mac and that also fails to show the issue. If it occurs again I'll archive the sketch and associated libraries before I touch anything, and then try to pin down some reliable steps to reproduce, otherwise I'd just be wasting your time.

@AndyA , I'd been "forcing a full recompile" by changing CPU speeds: the errors showed up at 600MHz and didn't at 528MHz, quite reliably (before today...). Maybe that was insufficient, though it's usually worked for me before. I agree, it's almost certainly some weird Arduino caching thing, quite likely associated with some other playing around I've been doing with the boards.local.txt and platform.local.txt. If that's it, I fear it'll be nigh-on impossible to reproduce, but I may be able to get a handle on the right workaround for others to use in the fufture.

Does the Arduino "IDE" still not have a button for a clean build?
You're such an optimist... but then apparently
In latest Arduino IDE 2.3.10, press Ctrl - Shift - R (or Command - Shift -R on Macintosh) to recompile all.
which isn't exactly a button and never will be. It was only introduced in 2.3.10, and might with luck get documented in, oh, 2.5.87? Certainly doesn't show up in a Google search.
 
Back
Top