The hardest warning to remove: "Warning C4005 'M_PI': macro redefinition", how to remove it?

Manda Rajo 141 Reputation points
2022-01-13T20:25:34.853+00:00

Hi, I read this https://github.com/robotology/community/discussions/81 but it's too big. I spent a lot of time to try to remove the Warning about M_PI but I always failed, so I show a screenshot to tell "What do you have in mind to try to hide the warning about M_PI?", because my C++ project is so big and all I have to do is to show screenshot.
164955-warning.png
There are many lines #include <math.h> in other source codes but as the screenshot tells, the warning is inside the project "imgui_backend" (file: math.h), and that project only has 2 files using that "math library" (imgui_internal.h and imgui_demo.cpp). So, do you think attempting to try repeating the same fix in all the #include <math.h> in other sources not in the indicated project will fix the problem? But there are A LOT of that line of math include, and it will ruin the internal source codes, I tried it at first but it didn't fix, it's too difficult because I'm not sure if I 100% did it or 95% did, too difficult because there are a lot of lines (#include <math.h>) everywhere.

I always did this every time I compile my project for this "Specific" problem that's very hard to fix:

  • Clean the solution and re-compile the project, because cleaning is sometimes a fix of bug on the C++ editor that's almost impossible to detect.

In the link I showed in the beginning, people say to #define _USE_MATH_DEFINES before #include <cmath>
The following doesn't solve the problem either because there's still the warning:

The code below is applied in those 2 files imgui_internal.h and imgui_demo.cpp:

   // Remove the Warning C4005 'M_PI': macro redefinition https://github.com/robotology/community/discussions/81  
   // Before:  
       //#include <math.h>     // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf  
   // After:  
       #undef _USE_MATH_DEFINES // Because it's already defined in the CMakeLists.txt.  
       #define _USE_MATH_DEFINES  
       #undef M_PI             // Attempting to force undefine it.  
       #include <cmath>        // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf  

But that doesn't remove the warning.

Developer technologies | C++
{count} votes

3 answers

Sort by: Most helpful
  1. Guido Franzke 2,191 Reputation points
    2022-01-14T10:41:37.56+00:00

    Hello,
    as far as I found out, math.h is included in imgui_internal.h already. So defining USE_MATH_DEFINES or undef M_PI after this include is too late (your include math.h is discarded because of the earlier imgui_internal.h include). So the warning is an earlier result of the include files.
    Try the following:

    • if you use stdafx.h: put #define USE_MATH_DEFINES at the end of your stdafx.h
    • if you don't use stdafx.h: put the define at the beginning of the source file.
      If it doesn't help: Try #define _USE_MATH_DEFINES instead of USE_MATH_DEFINES.
      Open a new source file and include step-by-step the imgui headers and math.h at different positions. When the warning occurs, try putting undef M_PI in between your include directives.

    If all of this does not help, search for #define M_PI in your project because IMGUI uses "IM_PI", not "M_PI". So there is another include of a header that defines M_PI (which is not math.h).

    Regards, Guido

    1 person found this answer helpful.

  2. Manda Rajo 141 Reputation points
    2022-01-13T22:37:50.38+00:00

    I tried to do it around SDL but I cannot do it, but the attempt below doesn't remove the warning, this is the first time I use screenshot instead of codes because it's too difficult to fix. And as you can see in the image, I searched the keyword "sdl.h" in the entire solution and only these 3 files who have it.

    before all #include <SDL.h> lines. Also undo other changes.

    164887-warning-with-sdl.png

    Just talking why I write ???????????????, it's my favorite keyword as a debugging because when I see it, then I will delete the lines with it to clean my codes and I will never forget, because if I don't write it then my codes will be full of debugging and won't be cleaned.

    And additionaly:

    I did this for the other files 'imgui_demo.cpp' and 'imgui_internal.h':

       #define _USE_MATH_DEFINES  
       #include <math.h>        // sqrtf, fabsf, fmodf, powf, floorf, ceilf, cosf, sinf  
       #undef M_PI  
    

    But the warning is not removed.


  3. Manda Rajo 141 Reputation points
    2022-01-14T16:49:19.497+00:00

    Thanks GuidoFranzke, I tried but I didn't know how to do it, I even put so many #undef M_PI everywhere around many #include <anything> but I couldn't fix the problem.
    I also attemted this on top of every imgui source files, but I failed:

       #define USE_MATH_DEFINES  
       #define _USE_MATH_DEFINES  
       #undef M_PI  
    

    As YujianYao explaind

    Could you please provide a minimal executable example? This will help people find and solve your problem.

    https://github.com/mandaxyzw/myengine_minimal

    The file to download is named "myengine_minimal.zip".

    Requirements:

    • Visual Studio 2015
    • Vulkan 1.2.182.0 (if you use the latest Vulkan then it will be failed to compile).

    Note:

    • I leave the "build" folder by "CMakeLists.txt" because it's one of the hardest to fix if missing system files happen, I just deleted the big files in it.

    Screenshot
    165128-myengine-minimal.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.