我在Ubuntu 12.04 x64上,使用Eclipse Indigo w/CDT插件,而我的g ++版本是4.7.3。我已经将编译器选项-std=c++11
和相同的选项添加到发现选项中(即使代码在写入时没有自动完成)。编译器选项不适用于无序集C++ 11功能
Eclipse以红色突出显示了以下部分,并给出警告。但是,程序运行时(忽略错误)将打印预期结果(Compiled with gcc 4.7\n 1 0\n
)。我该如何解决这个问题?
#include <unordered_set>
#include <iostream>
using namespace std;
int main()
{
unordered_set<int> s; // Symbol unordered_set cannot be resolved
cout << "Compiled with gcc " << __GNUC__ << '.' << __GNUC_MINOR__ << endl;
s.insert(0); // Method insert cannot be resolved
s.insert(1); // Method insert cannot be resolved
s.insert(0); // Method insert cannot be resolved
for(auto i = s.begin(); i != s.end(); ++i) cout << ' ' << (*i);
// Method begin and end cannot be resolved
cout << endl;
return 0;
}
这是Eclipse在命令行调用:
...$ gcc -E -P -v -dD -std=c++11 .../specs.c
Using built-in specs.
...
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04)
(无论如何,它应该没有必要存在)关闭我C
编译-std=c++11
编译器选项后 - 但保管它用于C++
编译器,打印到控制台的输出如下。
...$ g++ -E -P -v -dD -std=c++11 .../specs.cpp
Using built-in specs.
...
gcc version 4.7.3 (Ubuntu/Linaro 4.7.3-2ubuntu1~12.04)
而且,不管错误的Eclipse显示我的,输出还是一样,表现出无序的作品和被正确编译。这显然只是构建之前的一个问题。如果我的索引器在构建之前没有正确解析,那么这个集合在运行时如何工作?
可能你安装了多个版本的GCC,并且Eclipse以某种方式最终使用了错误的版本? (在你的代码中插入类似'std :: cout <<“编译的gcc”<< __GNUC__ <<'。'<< __GNUC_MINOR__ << std :: endl;'以便输出用于编译它的GCC版本。 ) – jogojapan
@jogojapan是的,我确实安装了多个版本的GCC,但是你的代码返回“Compiled with gcc 4.7” - 应该有C++ 11,对吗? – VF1
是的,没错。从4.7开始,C++ 11选项确实是“-std = C++ 11”(在4.6和更早的版本中它曾经是“-std = C++ 0x”)。所以,我不知道为什么它不起作用。 – jogojapan