【问题标题】:g++ Compiler Giving Strange Errors in Terminalg++ 编译器在终端中给出奇怪的错误
【发布时间】:2016-02-28 11:59:23
【问题描述】:

我最近在我的 Mac 上安装了 xcode 命令行工具。它的操作系统是 10.9.5 版本。为了测试 g++ 编译器,我创建了一个简单的“hello world”c++ 程序,将其命名为 program.cpp 并将其放在我的桌面上:

#include <iostream>

using namespace std;

int main()
{
    cout<<“Hello World”;
    return 0;
}

然后我打算创建一个program.cpp的编译版本,打开终端,将我的目录更改为桌面并输入以下命令:

g++ -o program program.cpp

我收到以下编译错误:

program.cpp:7:11: error: non-ASCII characters are not allowed outside of
      literals and identifiers
    cout<<“Hello World”;
          ^
program.cpp:7:14: error: use of undeclared identifier 'Hello'
    cout<<“Hello World”;
           ^
program.cpp:7:25: error: non-ASCII characters are not allowed outside of
      literals and identifiers
    cout<<“Hello World”;
                      ^
3 errors generated.

这是命令行工具的问题还是其他问题?任何帮助将不胜感激。

【问题讨论】:

  • 如果您输入了上述代码,请确保您的输入法是英文。

标签: c++ macos compiler-errors g++ command-line-tool


【解决方案1】:

您没有使用双引号:

“Hello World”

不等于:

"Hello World"

我猜这是由于从 PDF 或其他内容复制粘贴所致,因为 Word 等程序喜欢将“”更改为“”。

此外,如果您使用的是 Xcode 命令行工具,那么您实际上使用的是 clang 而不是 g++。

【讨论】:

  • 我在文本编辑中输入代码,但我不知道双引号字符问题。我会在 atom 中再试一次。至于g ++ clang的差异,我在别人的mac上都试过了,都可以工作。所以这可能不是问题。
  • 如果英语不是您的第一语言,那么您可能比我更了解这些问题是如何发生的。 clang 评论与问题无关,但您需要注意您实际上并没有使用 GNU 产品(例如,键入 g++ --version)。
  • @OrrenRavid 不要在 TextEdit 中编写代码,或者任何其他用于编写“普通”文本的代码。使用(程序员的)文本编辑器 - 有很多可供选择 - 或像 XCode 这样的 IDE。
  • 谢谢,很高兴知道。我将尝试使用其他文本编辑器解决我的问题,并在问题解决后选择此答案。
  • 这似乎是问题所在。我切换到一个“原子”,一个用于编程语言的文本编辑器,它编译正确。
【解决方案2】:

您的 Mac 上可能设置了“美国国际 - PC”输入法(假设您在 Mac 上),并且带有重音字符,导致在您预期 '"' 时插入 '"'。

enter link description here

【讨论】:

    猜你喜欢
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多