Error 'M_PI_2' was not declared


I've compiled OpenCascade from sources successfully.

But when add       #include <gp_Pnt.hxx>     in my application headers, I get the following error when I try to compile my application.

OpenCascade.src\src\gp\gp_Vec2d.lxx|77|error: 'M_PI_2' was not declared in this scope; did you mean 'M_PI'?

Any suggestion would be appreciated !





Kirill Gavrilov's picture

You have not shared any information about used compiler.
Considering that you are using Visual Studio - your code might include "math.h" without _USE_MATH_DEFINES defined:

Just including Standard_Real.hxx before your headers may help as it defines _USE_MATH_DEFINES.

John Whitaker's picture

Hi Kirill,

Thanks for your answer.  I'm using  GCC 9.2.0  (TDM-GCC)  and target is Win64.

Your suggestion was wise since it seems the issue comes from GCC 9.2.0 itself.

It also requires the -D_USE_MATH_DEFINES option.

Now compiling like a charm...  :)



Kirill Gavrilov's picture

It might be reasonable patching (contributing) an OCCT to define _USE_MATH_DEFINES also in case of gcc, not just msvc in Standard_Real.hxx.

John Whitaker's picture

A bit tricky, since M_PI (and some others) are well defined without -D_USE_MATH_DEFINES.

But M_PI_2 does requires -D_USE_MATH_DEFINES.


Kirill Gavrilov's picture

M_PI_2 is defined within the following #ifdef block in math.h:

> #if !defined(__STRICT_ANSI__) || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_USE_MATH_DEFINES)

Sample program shows that M_PI_2 is available on Linux because of _GNU_SOURCE macros defined by g++ for "systems using glibc".
MinGW does not define _GNU_SOURCE, but OCCT compiles fine because of usage of "-std=gnu++0x" compiler flag instead of "-std=c++0x".
This activates GNU extensions and undefines __STRICT_ANSI__ macros.
So that as alternative you may defined C++ standard version/extensions to MinGW consistent to how OCCT was built itself.

#include <stdio.h>
#include <math.h>

int main(void)
#ifdef __STRICT_ANSI__
  printf("NO __STRICT_ANSI__\n");

#ifdef _GNU_SOURCE
  printf("NO _GNU_SOURCE\n");

#ifdef M_PI_2
  printf("M_PI_2=%f\n", M_PI_2);
  printf("NO M_PI_2\n");
  return 0;
John Whitaker's picture

Understood !

I've replaced my app previous flag c++17 by    -std=gnu++0x;  this indeed defines  M_PI_2 without the need of -D_USE_MATH_DEFINES .

But, if you're using smart pointers with the syntax    std::make_unique< , this seems conflicting with the  -std=gnu++0x flag.

So, it's probably better just to add the  -D_USE_MATH_DEFINES  flag in your app compiler options.


Kirill Gavrilov's picture

-std=gnu++0x means C++0x (enabled by -std=c++0x) with GNU extensions, which was added to GCC before C++11 standard has been actually released.
This is a minimal requirement for building OCCT, but you can use a higher version if you like with -std=gnu++17 or something like this.

John Whitaker's picture

 I've already tried the option -std=gnu++17

It doesn't work ( M_PI_2 not defined) without the -D_USE_MATH_DEFINES flag.

But it's really no trouble at all to add -D_USE_MATH_DEFINES in apps compiling options.


Kirill Gavrilov's picture

Well, it is an unexpected behavior. g++ (tdm64-1) 5.1.0 works as expected with -std=gnu++17 flag.
Probably they have changed something in newer versions...