Firstly, I'm pretty new to C++. I believe that getline()
isn't a standard C function, so #define _GNU_SOURCE
is required to use it. I'm now using C++ and g++ tells me that _GNU_SOURCE
is already defined:
$ g++ -Wall -Werror parser.cpp
parser.cpp:1:1: error: "_GNU_SOURCE" redefined
<command-line>: error: this is the location of the previous definition
Can anyone confirm if this is standard, or is its definition hidden somewhere in my setup? I'm not sure of the meaning of the final line quoted.
The file's includes are as follows, so presumably it's defined in one or more of these?
#include <iostream>
#include <string>
#include <cctype>
#include <cstdlib>
#include <list>
#include <sstream>
Thanks!
I think g++, from version 3, automagically defines
_GNU_SOURCE
. This is supported by your third line in the error stating that the first definition was done on the command line (with nary a-D_GNU_SOURCE
in sight):If you don't want it,
#undef
it as the first line in your compilation unit. You may need it, however, in which case use:The reason you're getting the error is because you're re-defining it. It shouldn't be an error if you define it to what it already was. At least that's the case with C, it may be different with C++. Based on the GNU headers, I would say they're doing an implicit
-D_GNU_SOURCE=1
which is why it thinks you're re-defining it to something else.The following snippet should tell you its value provided you haven't changed it.
Getline is standard it is defined in two ways.
You can call it as a member function of one of streams as follows: This is the version defined in
or you can use the version defined in
for further reference http://www.cplusplus.com/reference/iostream/istream/getline.html http://www.cplusplus.com/reference/string/getline.html
I've always had to use one of the following in C++. Never had to declare _GNU_ anything before. I usually run in *nix so I usually use gcc and g++ as well.