The interface on my build system MacOS 10.6.3 for the POSIX math library is math.h, however on my target system the name of the interface file is cmath.h. At school we use cmath and I would like to be sure my project compiles when it is handed in, how is this achieved. The servers and workstations at school are x86 running Windows XP. The GCC is available on both platforms.
-
What is your "target system". What compiler, what operating system ?? Ask your teacher the differences between `cmath.h` and `math.h`? Is it `
` or ` – Basile Starynkevitch Jan 04 '12 at 21:24`? -
Sorry, I mean macos is my host system and windows XP is my target system. And if my prof knew the answer, then I wouldn't have to ask here. – awiebe Jan 04 '12 at 21:34
1 Answers
In the C++ standard, the math library functions are defined in two headers:
<cmath>
contains them in the namespace std
(e.g. std::sin
), while
<math.h>
contains them in the global namespace (so just sin
).
There are further differences between the two: while <math.h>
contains all the C math functions with distinct names for distinct types, such as
double sin(double);
float sinf(float);
long double sinl(long double);
etc., <cmath>
contains overloaded functions such as
namespace std {
double sin(double);
float sin(float);
long double sin(long double);
}
etc. (C++ libraries might additionally export sinf
from <cmath>
, but you can't rely on this in a portable program.)
Finally, the fabs
, fabsf
and fabsl
functions from the C standard library have become overloads of std::abs
in <cmath>
.
Though both headers are in the standard, you should really prefer <cmath>
, as <math.h>
is only there for backward compatibility with pre-standard C++ and C.
There's no such thing as <cmath.h>
in standard C++.

- 355,277
- 75
- 744
- 836
-
OK. Removed my answer rather than edit it, as you've covered the important bits here. I think a mention of the use of the
naming style for other standard libraries is worthwhile, but I'll leave that up to you. – Harper Shelby Jan 04 '12 at 21:37 -
@HarperShelby: put that in, near the end. I hope the OP reads that far, cause this is quite a list ;) – Fred Foo Jan 04 '12 at 21:39
-
The goal is to get good information out there - you can lead a horse to water and all that. – Harper Shelby Jan 04 '12 at 21:45
-
Ok so in C++ header file names should be specified without a .h, and this is in theory why I can't link against cmath, but there is no manpage on cmath (at least not on my system), so I don't know if it is properly installed and my laptop just died, so I'll check back later and see if it is still an issue. – awiebe Jan 04 '12 at 21:45
-
@awiebe: I've never seen a system that has manpages on the C++ library. Manpages tend to cover the C library only. – Fred Foo Jan 04 '12 at 21:46
-
On macos the C++ standard libraries are not located where I would expect them to be, this may be an error in my system configuration(my $PATH variable is missing a path), because running the gcc from the command line causes error, however creating an xcode project using commandline tool -> c++ -> stdc++ adds the appropriate paths to the target so that fixed it. – awiebe Jan 06 '12 at 05:28