2

While making a project compile across both GCC and Clang, I've noticed that functions like fabs were causing compilation issues on GCC, since I had never included <cmath> myself. I was developing on Clang, so I never noticed this, since the llvm <cstdlib> header file I was using includes <cmath> itself, seemingly without doing anything with it, while the GCC version does not.

Is there any reason for this? and is there anything I can do to make sure my code will compile across multiple standard libraries besides just trying it?

drewcassidy
  • 152
  • 1
  • 9
  • If I remember correctly, the standard allows a C++ implementation to include any standard header inside another one. This can sometimes make your code compile even if it did not include all the needed headers. One would wish implementations were more pedantic so that they can help in spotting such mistakes, and ensure more portability, but reality differs. – chi Feb 06 '21 at 11:26

1 Answers1

0

For every function you use, consult the C++ reference to find out which header you should include.

The reference for fabs says that you have to include <cmath>.

arrowd
  • 33,231
  • 8
  • 79
  • 110
  • Yes, thank you. I knew to do this, but checking every function in a large file and cross referencing it with the documentation isnt fun. Luckily I was linked by a friend to https://github.com/include-what-you-use/include-what-you-use to do that automatically – drewcassidy Feb 06 '21 at 08:37