1

I'm porting the Arduino framework on a NIOS II system which, depending on configuration (normal/small C library), may have or not have atof() function available. Naturally, String::toDouble() which is implemented using atof() cannot always be there either. However, I would still like to make it available for users with normal C library. Desired behavior:

  • small C library, user doesn't call String::toDouble() - the framework should compile
  • small C library, user calls String::toDouble() - the framework should fail to compile
  • normal C library user doesn't call String::toDouble() - the framework should compile
  • normal C library, user calls String::toDouble() - the framework should compile

How could I implement this? The choice between normal/small C library is implemented as a command line option for the linker, and as far as I can tell, no macro defining the choice is available to the application at compile time.

Current NIOS toolchain is based on GCC 5.3.0, but older devices are only supported by the old toolchain based on GCC 4.7.3. I would prefer a solution which would work on both.

Dmitry Grigoryev
  • 3,156
  • 1
  • 25
  • 53

1 Answers1

1

How could I implement this?

You can try to build with -ffunction-sections and link with --gc-sections.

String::toDouble() should be placed in its own section in the output file and then garbage collected by the linker if it is not actually used. See ld documentation:

--gc-sections

Enable garbage collection of unused input sections.

And gcc documentation:

-ffunction-sections

Place each function or data item into its own section in the output file if the target supports arbitrary sections.

ks1322
  • 33,961
  • 14
  • 109
  • 164
  • Thanks, that's what I ended up doing. The only thing that could be improved is the error message which says "undefined reference to `_localeconv_r`". Do you happen to have an idea how to produce a better diagnostic referring to `String::toDouble()`, or, ideally, a custom message saying the user should link against normal C library? – Dmitry Grigoryev Jan 21 '18 at 19:06
  • I was thinking of something like linker maps, but it seems you already [know](https://stackoverflow.com/a/48283182/72178) about them. – ks1322 Jan 21 '18 at 20:00
  • Linker maps is how I traced the issue back to `String::toDouble()`. What I would like to do is provide users of my framework with a better diagnostic, in case they don't know about linker maps. – Dmitry Grigoryev Jan 22 '18 at 08:24
  • I don't know any better way to do it. Perhaps framework users should also learn to use linker maps. – ks1322 Jan 22 '18 at 13:54