getline()在C ++ - _GNU_SOURCE不需要? [英] getline() in C++ - _GNU_SOURCE not needed?

查看:257
本文介绍了getline()在C ++ - _GNU_SOURCE不需要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是C ++的新手。我相信 getline()不是一个标准的C函数,因此需要 #define _GNU_SOURCE 才能使用它。我现在使用C ++和g ++告诉我 _GNU_SOURCE 已经定义:

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>


$ b

谢谢!

Thanks!

推荐答案

我认为g ++,从版本3,自动定义 _GNU_SOURCE 。这是你的第三行在错误中支持的第一个定义是在命令行上完成的(看到 -D_GNU_SOURCE ):

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):

<command-line>: error: this is the location of the previous definition

如果你不想要它, #undef 它作为编译单元的第一行。您可能需要它,但在这种情况下使用:

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:

#ifndef _GNU_SOURCE
    #define _GNU_SOURCE
#endif

得到错误的原因是因为您要重新定义它。如果你将它定义为它已经是什么,它不应该是一个错误。至少是C的情况,它可能与C ++不同。基于GNU头,我会说他们做一个隐式 -D_GNU_SOURCE = 1 这就是为什么它认为你是重新定义

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.

#define DBG(x) printf ("_GNU_SOURCE = [" #x "]\n")
DBG(_GNU_SOURCE); // first line in main.

这篇关于getline()在C ++ - _GNU_SOURCE不需要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆