Skip to content

Always define _USE_MATH_DEFINES#87

Open
robUx4 wants to merge 6 commits intovideolan:masterfrom
robUx4:math-defines
Open

Always define _USE_MATH_DEFINES#87
robUx4 wants to merge 6 commits intovideolan:masterfrom
robUx4:math-defines

Conversation

@robUx4
Copy link
Copy Markdown
Contributor

@robUx4 robUx4 commented Apr 10, 2026

The public headers use M_PI and M_PI_2 which are defined when including <cmath> (or math.h) and _USE_MATH_DEFINES is defined (and some other defines are possible as well: #if !defined(__STRICT_ANSI__) || defined(_POSIX_C_SOURCE) || defined(_POSIX_SOURCE) || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_USE_MATH_DEFINES))

In my LLVM 22 toolchain <string> includes <cmath>. And it's included before the local header in many cases (as well as most toolchain headers are included before the local headers). And even if it was not the case, an app using the public headers could be including <cmath> before including our headers. In that case M_PI and M_PI_2 won't be defined. So we need to make sure _USE_MATH_DEFINES is defined before using our headers.

robUx4 added 5 commits April 10, 2026 11:59
Depending on the order of includes math.h may be included before we have a chance to set the define
Because we don't control the order of includes and we use M_PI/M_PI_2 in public
headers we need to make sure including <cmath> will provide the proper defines.
We already include <cmath>
@robUx4
Copy link
Copy Markdown
Contributor Author

robUx4 commented Apr 10, 2026

The other alternative is to not use M_PI and M_PI_2 in headers but only in C++ files. But it seems a bit tricky and it may change the ABI.

Not to be confused with the C one that requires math.h.
@robUx4
Copy link
Copy Markdown
Contributor Author

robUx4 commented Apr 10, 2026

BTW, there are C++ variants for these math values, but it requires C++20.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant