This is GCC Bugzilla
This is GCC Bugzilla Version 2.20+
View Bug Activity | Format For Printing | Clone This Bug
preprocessor error! I wrote a simple C++ programing using GCC 3.2-cygwin, MingW, Linux, etc version as below. =================== #include<iostream> using namespace std; const double M_PI = 3.1415; int main() { cout << M_PI << endl; return 0; } =================== There're a parsing error in "M_PI". It has been defined in cmath(math.h) as: #define M_PI 3.14159265358979323846 But, I didn't include the <cmath> header file. After undefined the M_PI, no error be found. I think it's an error of preprocessor of GCC. For addition, I just included <iostream> header file, I can use all of methods declared in <cmath> header file. It's no compiling error on 2.95 or older version of GCC.
Subject: Re: New: parse error before numeric constant "is88071@cis.nctu.edu.tw" <gcc-bugzilla@gcc.gnu.org> writes: | I wrote a simple C++ programing using GCC 3.2-cygwin, MingW, Linux, etc version | as below. | =================== | #include<iostream> | using namespace std; | | const double M_PI = 3.1415; | | int main() | { | cout << M_PI << endl; | return 0; | } | =================== | | There're a parsing error in "M_PI". It has been defined in cmath(math.h) as: | #define M_PI 3.14159265358979323846 | | But, I didn't include the <cmath> header file. Agreed. However, <iostream> is permitted to include <cmath>. But <cmath> is not supposed to define M_PI, that we fail to provide a clean <cmath> is a bug in libstdc++. However what the right fix should be is not obvious. -- Gaby
This is not a bug as the ISO C++ standard say that any standard header can include any other standard headers so you include iostream which in turns includes cmath.h. The bug is that cmath.h should not define M_PI which is not included in the C++ standard but is included in XOPEN/SVID.
Subject: Re: parse error before numeric constant "pinskia@physics.uc.edu" <gcc-bugzilla@gcc.gnu.org> writes: | This is not a bug as the ISO C++ standard say that any standard header can include any other | standard headers so you include iostream which in turns includes cmath.h. The bug is that | cmath.h should not define M_PI which is not included in the C++ standard but is included in | XOPEN/SVID. That is untrue. XOPEN/SVID does not say <cmath> should define M_PI. The issue isn't not that simple. We should just resolve this as of being very low priority bug to fix. -- Gaby
Downgrade priorities. This code is just so unnecessary that it can't be important to fix it. Also change category to libstdc++. W.
This is a bug, see mine and gdr's anyalsis.
The bug is that the C++ front end implicitly #defines _GNU_SOURCE. In glibc (I have version 2.3.2 here) this, amongst others, enables _BSD_SOURCE (see /usr/include/features.h). That in turn enables non-standard symbols like M_PI in math.h. Hence the failure. As a result one can argue that this is a C++ front end bug... however: gcc/config/linux.h tells us that: /* The GNU C++ standard library requires that these macros be defined. */ #undef CPLUSPLUS_CPP_SPEC #define CPLUSPLUS_CPP_SPEC "-D_GNU_SOURCE %(cpp)" ... so this may be a libstdc++ issue after all. The #define is also present for other configuration files in gcc/config/* and subdirectories. I googled and searched the libstdc++ archives and Changelog files, but did no find the reason why _GNU_SOURCE is in fact needed by gnu's libstdc++. I just removed the -D_GNU_SOURCE from gcc/config/linux.h (on i686-suse-linux) and bootstrapped gcc (HEAD) with languages=c,c++. That went just fine and (obviously) fixed the problem reported here. Could some libstdc++ guru explain why this define is actually needed?
(In reply to comment #6) > The bug is that the C++ front end implicitly #defines _GNU_SOURCE. > [..] > Could some libstdc++ guru explain why this define is actually needed? I am no libstdc++ guru, but this: echo "#include <cstdlib>" | g++ -x c++ -U _GNU_SOURCE -c - produces: /usr/include/c++/4.1.2/cstdlib:162: error: ‘::lldiv_t’ has not been declared /usr/include/c++/4.1.2/cstdlib:168: error: ‘::_Exit’ has not been declared /usr/include/c++/4.1.2/cstdlib:175: error: ‘::llabs’ has not been declared /usr/include/c++/4.1.2/cstdlib:177: error: ‘lldiv_t’ does not name a type /usr/include/c++/4.1.2/cstdlib:181: error: ‘::lldiv’ has not been declared /usr/include/c++/4.1.2/cstdlib:196: error: ‘::strtof’ has not been declared /usr/include/c++/4.1.2/cstdlib:197: error: ‘::strtold’ has not been declared /usr/include/c++/4.1.2/cstdlib:203: error: ‘__gnu_cxx::lldiv_t’ has not been declared /usr/include/c++/4.1.2/cstdlib:205: error: ‘__gnu_cxx::_Exit’ has not been declared /usr/include/c++/4.1.2/cstdlib:208: error: ‘__gnu_cxx::llabs’ has not been declared /usr/include/c++/4.1.2/cstdlib:209: error: ‘__gnu_cxx::div’ has not been declared /usr/include/c++/4.1.2/cstdlib:210: error: ‘__gnu_cxx::lldiv’ has not been declared /usr/include/c++/4.1.2/cstdlib:213: error: ‘__gnu_cxx::strtof’ has not been declared /usr/include/c++/4.1.2/cstdlib:216: error: ‘__gnu_cxx::strtold’ has not been declared
*** Bug 35557 has been marked as a duplicate of this bug. ***
Update summary.