【问题标题】:Eclipse C.D.T. can't recognize standard functions or "NULL"Eclipse C.D.T.无法识别标准函数或“NULL”
【发布时间】:2014-05-02 17:05:22
【问题描述】:

我已经遇到这个问题好几个星期了,一直没能找到解决办法。

当尝试使用 Eclipse 或尝试通过终端使用普通 GCC 或 G++ 进行编译时,有许多标准函数(加上“NULL”变量)无法识别,包括 to_string 方法。

我尝试向程序添加许多不同的标头以尝试找到正确的文件,但无济于事。我已经尝试过#including <string>, <stdio.h>, <stddef.h>, <cstdlib>,以及我在论坛帖子中可以找到的几乎任何其他可能包含 to_String 函数的标准标题。我什至尝试了所有这些带有 AND 而不带有“.h”扩展名的#includes。尽管如此,无论我尝试什么,都无法识别 to_string 和 NULL(以及其他方法)。

我浏览过很多论坛和很多论坛帖子,尝试过很多解决方案,包括this onethis onethis onethis onethis one 等等。尽管如此,我还是没有找到解决办法。

我已经尝试卸载并重新安装 Eclipse C.D.T.、Eclipse as a whole、GCC 和 G++。我尝试将 -std=c++11 和/或 -std=c++99 标志添加到 GCC 命令或 g++ 命令。我尝试在 Eclipse 中使用 Linux GCC、Cross GCC 和其他版本的 GCC 进行构建。

我正在运行带有 J.D.T. 的 Eclipse 3.8。和 C.D.T.安装在 64 位 Linux Mint 16 Petra 上的软件包。

如果有人能帮我解决这个问题,“grateful”不会正确地表达我对你的感激之情。

编辑: 这是gcc -v的输出:

Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.8.1-10ubuntu9' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.1 (Ubuntu/Linaro 4.8.1-10ubuntu9)

这里是有错误的代码示例。请记住,我已尝试添加其他当前未显示的#include,我有理由相信缺少的#include 不是问题。

#include <string>
#include <stdio.h>
#include <iostream>

using namespace std;

int Month[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

string x;

class Date_Time {
private:
    int minute, hour, day, month, year;
public:

    Date_Time(int minute, int hour, int day, int month, int year) {
        this->minute = minute;
        this->hour = hour;
        this->day = day;
        this->month = month;
        this->year = year;
    }

    string toString() {
        string min = to_string(minute);
        if (min.length() == 1)
            min = '0' + min;

        return to_string(month) + "/" + to_string(day) + "/" + to_string(year)
                + " " + to_string(hour) + ":" + min;

    }

    void addMinutes(int min) {

        minute = min + minute;
        if (minute > 59) {
            minute = minute - 60;
            hour = hour + 1;
        }
        if (hour > 24) {
            hour = hour - 24;
            day = day + 1;
        }

        if (day > Month[month])
            day = day - Month[month];
        month = month + 1;

        if (month > 12) {
            month = 1;
            year = year + 1;

        }
    }

    int getYear() {
        return year;
    }

    int getMonth() {
        return month;
    }

    int getDay() {
        return day;
    }

    int getHour() {
        return hour;
    }

    int getMinutes() {
        return minute;
    }

};

编辑:下面的 cmets 的总结是我们已经确定原因是 Eclipse 没有识别 C++11 标准函数和关键字。我使用他们的 C/C++ 插件在我的系统上安装了 NetBeans,并且发生了完全相同的错误。尽管在项目配置的多个位置添加了-std=c++11 标志,但我似乎无法让 NetBeans 或 Eclipse 识别 C++11 功能。

截至目前,Eclipse 终于可以编译而不会出现 C++11 错误;但是,在代码窗口中,C++11 函数和特性仍然被标记为带有红色下划线的错误,因此问题仍未完全解决。我只需要有人告诉我如何让 Eclispe 和/或 NetBeans 识别其错误解析器中的 C++11 功能。

再次感谢您提供的任何帮助。

【问题讨论】:

  • 你能分享一下你的gcc版本吗(gcc -v)。
  • 请包含一个示例源文件,可用于在您的帖子中重现您的问题。
  • 我的版本是4.8.1。我将编辑原始问题以显示 gcc -v 的整个输出,因为它太大而无法粘贴到此评论中。
  • 我还会添加一个代码示例。
  • 我添加了一个代码示例和 gcc -v 的完整输出。此外,除了 to_string 和 NULL 之外,其他无法识别的方法之一是 strcmp/stricmp。

标签: c++ linux eclipse gcc null


【解决方案1】:

尝试编译你的源代码

g++ -std=c++11 ...

根据to_string(),它是C++ 2011 的一个特性。


在 Eclipse Kepler 中,您可以通过选择进行设置

项目 >> 属性 >> C/C++ 构建 >> 设置 >> 工具设置 >> GCC C++ 编译器 >> 方言

并为Language standard 选择ISO C++11 (-std=c++0x)

您应该在C/C++ Build 处为Configuration 选择[All configurations]

【讨论】:

  • 我之前做过,但我再次尝试以确保。很难说它在终端中是否有效,但 Eclispe 仍然调用 to_String 一个错误,声称“函数 'to_string' 无法解析”。
  • @REALDrummer Eclipse 提供的g++ 选项是什么?
  • 在项目 >> 属性 >> C++ 构建 >> 设置 >> 工具设置 >> GCC C++ 编译器 >> 杂项,我将标志列为-c -fmessage-length=0 -std=c++11
  • @REALDrummer 试试这个:Project >> Properties >> C++ Build >> Settings >> Tool Settings >> GCC C++ Compiler >> Dialect >> Language standard,然后选择ISO C++11
  • 我将我的配置更改为“[所有配置]”,但它并没有明显改变任何东西。至于“语言标准”设置,这里的 Eclipse 3.8 似乎与 Kepler 不同。我去了你提到的“GCC C++ Compiler”设置目录,但没有找到“方言”或“语言标准”设置。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-22
  • 2021-07-01
  • 2015-07-13
  • 2013-11-18
  • 2012-04-19
相关资源
最近更新 更多