0

I am working with an embedded ARM device and am compiling a program. I want to minimize the memory used.

I use the GCC-ARM toolsuite to build the program. However on linkin, I get the error: /opt/gcc-arm-none-eabi-10.3-2021.10/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/bin/ld: /opt/gcc-arm-none-eabi-10.3-2021.10/bin/../lib/gcc/arm-none-eabi/10.3.1/../../../../arm-none-eabi/lib/thumb/v7e-m+fp/hard/libstdc++.a(math_stubs_long_double.o): in function acosl': math_stubs_long_double.cc:(.text.acosl+0x0): undefined reference to acos' This appears not just for acos, the same error is repeated for all math functions. Due to this I am forced to link the math library via the -lm flag. The code I am using is spread over multiple files (which is why I cannot paste it here) and I have gone through all the included headers and have found no reference to any math library function. Due to memory constraints, I hope to remove this dependency. Is there any way to do this?

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • 2
    Please show your link command – n. m. could be an AI Jul 06 '23 at 20:28
  • Your compile command could be missing `-ffunction-sections -fdata-sections` or the standard C library was built without it. And the linker command could be missing `-Wl,--gc-sections`. – Codo Jul 06 '23 at 20:34
  • Yes, first off are you linking in something you dont need. Second if you end up with some baggage you dont want with something you need, then above will help remove that. – old_timer Jul 06 '23 at 21:42

1 Answers1

0

A map file also shows why libraries are included. For example,

../libm.a(lib_a-sf_finite.o)  ./adcManager.o (finitef)

By linking with -lm and generating a map file, you can find the answer for yourself.

In your case, you are using libstdc++. Using functionality like std::cout, etc can pull in formatting code for floating point. The text is giving a hint.

libstdc++.a(math_stubs_long_double.o): in function acosl': math_stubs_long_double.cc:(.text.acosl+0x0): undefined reference to acos

You need to look at a map file to see why 'math_stubs_long_double.cc' is being included and trace the chain until you find a call from libstdc++ to your source.


--gc-sections and other techniques can reduce overhead. However, this is a rather vast topic. Various specialized (and limited) C libraries exist to try and minimize memory footprint, often at the expense of functionality and runtime performance.

I am not so aware of libstdc++ that is minimally functional.

See also: Undefined reference to cxa..

artless noise
  • 21,212
  • 6
  • 68
  • 105
  • Why would floating point formatting code, or indeed anything else in the C++ standard library, need to calculate trigonometric functions? – n. m. could be an AI Jul 07 '23 at 19:12
  • Because the floating point format draws in one function which draws in another function, etc. Trigonometric operations are used for many purposes in mathematics. Also, this is just a suggestion as to the origin. The OP's answer is in the map file and the answer addresses this in the first few sentences. I was not trying to say that the formatting code would definitely draw in trig functions, the origin could be something else. – artless noise Jul 07 '23 at 22:08
  • It is possible that with C++, the stream operators are part of another class, like 'std::complex'. However, with C, `printf()` can draw in floating point formatting which will perform operations such as testing a sign, determining decimal place, etc. These functions in turn maybe in the same object which draws in trig functions. – artless noise Jul 09 '23 at 15:35