expected unqualified-id before numeric constant

What could be the source of this compilation error?


mathconst.hpp:2:14: error: expected unqualified-id before numeric constant
 const double M_PI   = 3.14159265358979323846;
               ^


This definition seems innocuous to me and the line is part of a math constant definition file included in a C++ library that compiles without trouble under Windows VC++17 and Linux g++/c++17.

The error however comes during a GCC compilation controlled by Cython. Cython is a tool to wrap C/C++ code so that it can interface with a Python interpreter. Cython generates the wrapping code and sends it to GCC. It is a bit of a black box to me so I am not sure why I am getting an error now.

Here is the GCC call:


gcc -pthread -Wno-unused-result -Wsign-compare -DDYNAMIC_ANNOTATIONS_ENABLED=1 
-DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector 
--param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC 
-I/usr/include/python3.6m -c PyLidar.cpp -o build/temp.linux-x86_64-3.6/PyLidar.o 
-std=c++17
Last edited on
I suspect M_PI is already defined somewhere else, as a macro.
Yes, that's exactly what I found.
It is defined in <cmath>. Previously I had #define _USE_MATH_DEFINE in my code to get those definitions but that doesn't work in Visual Studio. So to make my code portable I had remove the #define _USE_MATH_DEFINE and instead created this const definition file. For some reason, however the macros contunue to be defined; not sure why.
How about:
1
2
3
4
5
6
7
#include <cmath>

#ifdef M_PI
#undef M_PI
#endif

const double M_PI   = 3.14159265358979323846;

but that doesn't work in Visual Studio


It works just fine in VS. The problem is when the #define is encountered, which is often before you think it is.

Make sure that the very first line of your file, before any #includes, says #define _USE_MATH_DEFINES (note the ā€˜Sā€™ on the end of that).

Unless you specify a specific standard, compilers will tend to include the math defines like M_PI. By providing the #define you override the standards-compliant behavior caused by -std=c++17. I suspect your invocation of cl did not specify a specific standard, and so it was not an issue.

Hope this helps.
Topic archived. No new replies allowed.