I'm trying to upgrade an application to C++17 and am getting the following compiler error:
error: ‘__BEGIN_NAMESPACE_STD’ does not name a type
I'm using gcc (GCC) 8.2.0 and compiling with the following command:
g++ -std=c++17 variant.cpp -o variant
Here's a small test program:
#include <iostream>
#include <variant>
using v_t = std::variant<int, double>;
int main(int argc, char const* argv[]) {
v_t foo = 5;
printf("foo contains %d\n", *std::get_if<int>(&foo));
std::cout << "Success" << std::endl;
return 0;
}
And here's the error I get (below). Note that how to enable __BEGIN_NAMESPACE_STD in stdlib.h discusses a similar issue but the proposed solution (include C++ headers, not C headers such as stdlib.h) is something I'm already doing.
I am able to get this code compiling on my OSX laptop, possibly because I have a newer libc installed by default. However, when I run on my linux machine I get these errors. On the Linux machine, I am using the following linker/glibc version: ldd (Ubuntu EGLIBC 2.19-0ubuntu6.14) 2.19
I'm continuing to debug this but would appreciate any pointers in the right direction.
In file included from /toolchains/gcc-8.2.0/include/c++/8.2.0/cstdlib:75,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/ext/string_conversions.h:41,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/bits/basic_string.h:6391,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/string:52,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/bits/locale_classes.h:40,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/bits/ios_base.h:41,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/ios:42,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/ostream:38,
from /toolchains/gcc-8.2.0/include/c++/8.2.0/iostream:39,
from variant.cpp:1:
/usr/include/stdlib.h:95:1: error: ‘__BEGIN_NAMESPACE_STD’ does not name a type
__BEGIN_NAMESPACE_STD
^~~~~~~~~~~~~~~~~~~~~
/usr/include/stdlib.h:101:5: error: ‘div_t’ does not name a type; did you mean ‘pid_t’?
} div_t;
^~~~~
pid_t
/usr/include/stdlib.h:112:1: error: ‘__END_NAMESPACE_STD’ does not name a type
__END_NAMESPACE_STD
^~~~~~~~~~~~~~~~~~~
/usr/include/stdlib.h:121:5: error: ‘lldiv_t’ does not name a type; did you mean ‘ldiv_t’?
} lldiv_t;
^~~~~~~
-I
or-isystem
flags. It could also be helpful to look atg++ -E
output to determine which files were included from where. – Enigmatic