简体   繁体   English

g ++不包含它说包含在C ++ 11中的文件?

[英]g++ does not include files it says it includes for C++11?

Short version 简洁版本

When I compile even a simple code using a feature of the C++11 standard (the std::stod function), GCC 4.9.1 fails with the following error: 当我使用C ++ 11标准的功能( std::stod函数)甚至编译一个简单的代码时,GCC 4.9.1都会失败,并出现以下错误:

example.cpp: In function 'int main()':
example.cpp:10:18: error: 'stod' is not a member of 'std'
   double earth = std::stod (orbits,&sz);
                  ^
example.cpp:11:17: error: 'stod' is not a member of 'std'
   double moon = std::stod (orbits.substr(sz));
                 ^

What? 什么?

The command I use is g++ -std=c++11 example.cpp . 我使用的命令是g++ -std=c++11 example.cpp

This is the test code (which compiles fine on other systems): 这是测试代码(在其他系统上可以正常编译):

// stod example from http://www.cplusplus.com/reference/string/stod/
#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main ()
{
  std::string orbits ("365.24 29.53");
  std::string::size_type sz;     // alias of size_t

  double earth = std::stod (orbits,&sz);
  double moon = std::stod (orbits.substr(sz));
  std::cout << "The moon completes " << (earth/moon) << " orbits per Earth year.\n";
  return 0;
}

details 细节

I am using a version of GCC 4.9.1 I compiled myself on two different clusters running CentOS 6.5 (I use the modules system on stuff in my home dir since I'm not an admin). 我使用的是GCC 4.9.1版本,是在运行CentOS 6.5的两个不同的群集上编译自己的(由于我不是管理员,所以在主目录中的模块上使用了模块系统)。

I will call them cluster 1 and cluster 2 : cluster 1 is where the failure happens. 我将它们称为群集1和群集2 :发生故障的是群集1

The GCCs were compiled in the same way and at the same time, and loaded using identical module files (save for a minor difference in the base path). GCC是以相同的方式和时间进行编译的,并使用相同的模块文件进行加载(基本路径略有不同)。 The installations are, as far as I can easily check, identical (the same include files exist on both clusters, and have the same contents). 据我可以轻松检查,这些安装是相同的(两个群集中都存在相同的包含文件,并且具有相同的内容)。

The output from g++ -v is the same on both clusters (again, except for the install path): g++ -v的输出在两个集群上都是相同的(同样,除了安装路径):

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/home/andyras/bin/gcc-4.9.1/libexec/gcc/x86_64-unknown-linux-gnu/4.9.1/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.9.1/configure --prefix=/home/andyras/bin/gcc-4.9.1 --enable-languages=c,c++,fortran
Thread model: posix
gcc version 4.9.1 (GCC)

g++ -v using the system GCC gives the same output on both clusters, except on cluster 1 it says it is gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) and on cluster 2 says gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC) 使用系统GCC的g++ -v在两个群集上均提供相同的输出,除了群集1上说它是gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) ,群集2上说它是gcc version 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)

I am trying to debug using g++ -std=c++11 -save-temps -MD example.cpp for more info... this gives some clues, but I don't know where to go from here. 我正在尝试使用g++ -std=c++11 -save-temps -MD example.cpp进行调试,以获取更多信息...这提供了一些线索,但是我不知道从这里出发。

The intermediate ( .ii ) files on cluster 1 are missing some lines, for example (excerpt from diff ing the .ii files): 中间体( .ii在簇1)文件丢失一些线,例如(摘自diff荷兰国际集团的.ii文件):

< # 277 "/opt/gcc-4.9.1/include/c++/4.9.1/cwchar" 3
---
> # 277 "/home/andyras/bin/gcc-4.9.1/include/c++/4.9.1/cwchar" 3
961,963c934,936
<   using std::wcstold;
<   using std::wcstoll;
<   using std::wcstoull;
---
> 
> 
>

As I interpret it, GCC on both clusters tries to include files like cwchar , but on cluster 1 there are blank lines instead of things being defined. 按照我的解释,两个群集上的GCC都尝试包含cwchar文件,但是在群集1上有空白行,而不是定义行。 On cluster 2 the stod function is in the intermediate file, but not on cluster 1 . stod函数在群集2上位于中间文件中,但不在群集1上

Could it be a preprocessor error? 可能是预处理器错误?

Now looking at the .d (dependency) files, I also see a concrete difference. 现在查看.d (依赖性)文件,我也看到了具体的区别。 There are some files listed on cluster 2 that are not listed on cluster 1 . 群集2上列出了一些文件,而群集1上未列出。 Here is the list (I processed the contents of the .d files to account for the different base paths; // stands in for the install path): 这是列表(我处理了.d文件的内容以说明不同的基本路径; //表示安装路径):

85a86,108
> //gcc-4.9.1/include/c++/4.9.1/ext/string_conversions.h
> //gcc-4.9.1/include/c++/4.9.1/cstdlib
> /usr/include/stdlib.h
> /usr/include/bits/waitflags.h
> /usr/include/bits/waitstatus.h
> /usr/include/sys/types.h
> /usr/include/sys/select.h
> /usr/include/bits/select.h
> /usr/include/bits/sigset.h
> /usr/include/sys/sysmacros.h
> /usr/include/alloca.h
> //gcc-4.9.1/include/c++/4.9.1/cstdio
> /usr/include/libio.h
> /usr/include/_G_config.h
> /usr/include/bits/stdio_lim.h
> /usr/include/bits/sys_errlist.h
> //gcc-4.9.1/include/c++/4.9.1/cerrno
> /usr/include/errno.h
> /usr/include/bits/errno.h
> /usr/include/linux/errno.h
> /usr/include/asm/errno.h
> /usr/include/asm-generic/errno.h
> /usr/include/asm-generic/errno-base.h

I was curious if cpp was looking for includes in all the wrong places, but this seems legit ( cpp -v ): 我很好奇cpp是否在所有错误的位置查找包含,但这似乎是合法的( cpp -v ):

#include <...> search starts here:
 /home/andyras/bin/gcc-4.9.1/include
 /home/andyras/bin/gcc-4.9.1/include/c++/4.9.1/
 /home/andyras/bin/gcc-4.9.1/include/c++/4.9.1/x86_64-unknown-linux-gnu/
 /home/andyras/bin/gcc-4.9.1/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/include
 /usr/local/include
 /home/andyras/bin/gcc-4.9.1/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/include-fixed
 /usr/include
End of search list.

This has been a very frustrating couple of hours trying to track down the source of the problem. 试图找出问题的根源已经非常令人沮丧的几个小时。 I could, of course, use something like atof(myString.c_str()) instead of std::stod , but I am wondering if there is an underlying issue that will foul up future projects using other bits of C++11. 我当然可以使用诸如atof(myString.c_str())东西来代替std::stod ,但是我想知道是否存在一个潜在的问题,该问题会使将来使用C ++ 11的其他项目混为一谈。

Any more clues or insight would be very much appreciated. 任何更多的线索或见解将不胜感激。

You have already done all the necessary footwork to determine the root cause. 您已经完成了所有必要的步骤来确定根本原因。 You already know the answer: you have posted clear differences between how custom gcc installs behave on your "cluster 1" and "cluster 2", a non-working and a working build. 您已经知道答案了:在“集群1”和“集群2”上,自定义gcc安装的行为方式(工作正常且工作正常)之间已发布了明显的区别。

So, something is obviously different between these two gcc installs; 因此,这两个gcc安装之间显然有所不同。 if one compiles, links, and runs some code, and the other one chokes on the exact same piece of code. 如果其中一个编译,链接并运行一些代码,而另一个则阻塞完全相同的代码。 Unfortunately, it's hard to pinpoint where things go off the rails. 不幸的是,很难确定事情出轨的地方。 Having built gcc myself before, previously, I can say that gcc is a hellishly difficult, and very fickle package to install. 以前,我自己曾经构建过gcc,所以我可以说gcc非常难以安装,而且变化无常。 For example, after several days chasing a mysterious failure when linking some C++ code, I ended up chasing it down to gcc picking up the wrong version of binutils, in gcc's configure script, and internally turning off some obscure feature, that eventually manifested itself as a link failure not of gcc, but of stuff that got built with gcc. 例如,在几天后,在链接某些C ++代码时遇到了神秘的故障,我最终将其归结为gcc,在gcc的configure脚本中选择了错误版本的binutils,并在内部关闭了一些晦涩的功能,最终表现为链接失败不是gcc,而是gcc内置的链接失败。 gcc itself got built, and installed, with nary a complaint, but it was broken. gcc本身是在没有投诉的情况下建造和安装的,但是它坏了。

So, I find it completely unsurprising, and entirely plausible, the proposition that gcc itself got built and installed without an apparent issue, but it's broken, and will choke on valid code, in this fashion. 因此,我发现gcc本身是在没有明显问题的情况下构建和安装的,这一点完全不足为奇,而且完全合理。

My guess would be that your busted gcc build is using a broken default include path -- it's looking for headers in /usr/include , instead of your own custom installation directory. 我的猜测是,您破坏的gcc构建正在使用损坏的默认包含路径-它正在/usr/include查找标头,而不是您自己的自定义安装目录。 That's the most likely answer, but it could really be anything. 那是最可能的答案,但实际上可以是任何东西。

Well, you obviously have (like you wrote yourself) two different versoins of gcc (one time 4.4.7-3 and the other 4.4.7-4), and It seems that they have (even though slightly) differences in compiling the code. 好吧,您显然有(如您自己编写的)gcc的两个不同版本(一次为4.4.7-3,另一个为4.4.7-4),而且似乎它们在编译代码时有(即使略有不同)差异。

As I've read somewhere, the C++11-standard is not yet included completely in all compilers, so this seems to be the cause on the one Cluster. 正如我在某处所读到的,C ++ 11标准尚未完全包含在所有编译器中,因此这似乎是一个集群的原因。 The other has (you might know why) a version where more of the new standard is included and since you want you use exactly this feature I'd recommend installing this version on the other Cluster too. 另一个具有(您可能知道为什么)的版本,其中包含更多的新标准,并且由于您希望完全使用此功能,因此我建议您也将此版本安装在另一个群集上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM