Abs, rand, RANDMAX, etc. functions on OS X not found?


#1

I cannot build my project on OS X due to missing:

abs()
rand()
RAND_MAX (this is a macro)
atoi() (xcode suggests stoi)
atof() (xcode suggests stof)


#3

Well a quick google search should show you which C headers to include for those functions!

You shouldn’t expect JUCE to include any C headers for you, and would encourage you not to use any of these crusty old C library functions anyway. The modern C++ library provides far far better things you can use instead. (Not to mention the fact that JUCE itself also provides Random, String->int conversions, etc)


#4

Thanks jules! Your response is helpful to the person who is responsible for this code. Also it’s old code.

Edit: I just didn’t understand why this project would build in visual studio but not xcode, if it’s just a matter of includes.


#5

All compilers have a different implementation of the standard libraries. As such, sometimes some headers are included, sometimes not.
There was a big change in gcc at some point when they rethought their header hierarchy and lots of code stopped compiling because people were not included the proper headers but they were previously included indirectly.
But as Jules said, you should not use the functions you listed, but instead:

  • std::abs (include cmath)
  • use boost::random or JUCE Random class, the C one is only generating weak randomness
  • std::stoi and std::stof in conjunction with std::to_string converted from and to strings and not char*, so they don’t have some issues atoi and atof have with corrupted strings.