Search code examples
c++xcodec++11librariescmath

Libraries inclusion,while coding in C++ in XCode


While doing my class assignment in XCode I faced the following: The program I created, compiles and runs with no errors and works properly without included libraries like cstdlib & ctime, even though it contains rand and time functions in it. As well, chrono, string and cctype library features like .length() or isdigit seem to be working well without those libraries included too. The only library my program wasn’t compiling without was cmath, because it contained fabs function in it.

How is that possible? Is it because of the fact that XCode uses Clang compiler?


Solution

  • Including cstdlib, ctime, chrono, string or cctype will only add builtin declarations. See http://en.cppreference.com/w/cpp/header for a list of existing header files. Now, for all of those, there is the rule that each of them can include any other of them indirectly.

    Concerning your question why you have to include cmath but not others, recheck your code, I'm pretty sure that others are included. In particular iostream or fstream tends to pull in many other headers indirectly, in particular string or cctype. However, it probably doesn't require any floating point math functions, neither directly nor indirectly, so it won't pull in cmath.