__wrap__free_rが無いと言われたら

MbedのプロジェクトをGCC環境にエクスポートしてビルドすると、リンクの段階で下記のようなエラーが発生する場合があります。(どういう場合に発生するのか不明。)

undefined reference to `__wrap__free_r'
undefined reference to `__wrap__malloc_r'
undefined reference to `__wrap__calloc_r'
undefined reference to `__wrap__realloc_r'

この場合、リンカのオプションから対応する下記のものを削除すればエラーが解消します。

-Wl,--wrap=_free_r
-Wl,--wrap=_malloc_r
-Wl,--wrap=_calloc_r
-Wl,--wrap=_realloc_r

Eclipse系のIDE、例えばSW4STM32の場合、[Project] > [Properties] > [C/C++ Build] > [Settings] > [Tool Settings] > [MCU G++ Linker] > [Miscellaneous] の [Linker flags]で設定できる。

f:id:licheng:20200516204832p:plain