【问题标题】:Mac Terminal Compiling ErrorsMac 终端编译错误
【发布时间】:2017-06-09 08:52:42
【问题描述】:

注意:我确实安装了 xcode 以及它的命令行工具。

gcd.cpp:6:20:致命错误:iostream:没有这样的文件或目录

是我在终端运行时遇到的错误:

g++ gcd.cpp -o gcd -g -Wall -Werror -pedantic-errors -fmessage-length=0

我不知道如何解决这个问题。有人知道怎么解决吗?

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

using namespace std;

int gcdRecur(int a, int b) {
    if(b == 0)
        return a;
    else    
        return gcdRecur(b, a % b);
} 

int gcdIter(int a, int b) {
    int z;
        while(b != 0) {
            z = b;
            b = a%b;
            a = z;
        }
        return a;
}

int main(int argc, char *argv[]) {
    if(argc != 3){
        printf("Usage: %s <integer m> <integer n>", argv[0]);
        return -1;
    }
    string m_str = (string) argv[1];
    string n_str = (string) argv[2];
    istringstream iss;
    int m = 0, n = 0;
    iss.str(m_str);
    // Check if the first argument is an integer
    // by giving the istringstream 0
    if(!(iss >> m)){
        cerr << "Error: The first argument is not a valid integer." << endl;
        return -1;
    }
    iss.clear();
    iss.str(n_str);
    if(!(iss >> n)){
        cerr << "Error: The second argument is not a valid integer." << endl;
        return -1;
    }
    int iter = gcdIter(m, n);
    int recur = gcdRecur(m, n);
    printf("Iterative: gcd(%d, %d) = %d\nRecursive: gcd(%d, %d) = %d\n", m, n, iter, m, n, recur);
    fflush(stdout);
}

更新

运行命令:

g++ -H gcd.cpp -o gcd -g -Wall -Werror -pedantic-errors -fmessage-length=0

我现在得到这个输出(如果我切换它和 iostream 的顺序,它会对 sstream 产生同样的错误):

. /usr/include/stdio.h
.. /usr/include/sys/cdefs.h
... /usr/include/sys/_symbol_aliasing.h
... /usr/include/sys/_posix_availability.h
.. /usr/include/Availability.h
... /usr/include/AvailabilityInternal.h
.. /usr/include/_types.h
... /usr/include/sys/_types.h
.... /usr/include/machine/_types.h
..... /usr/include/i386/_types.h
.... /usr/include/sys/_pthread/_pthread_types.h
.. /usr/include/sys/_types/_va_list.h
.. /usr/include/sys/_types/_size_t.h
.. /usr/include/sys/_types/_null.h
.. /usr/include/sys/stdio.h
.. /usr/include/sys/_types/_off_t.h
.. /usr/include/sys/_types/_ssize_t.h

【问题讨论】:

  • 在 XCode 中查找预定义的标志。您可能需要指定标准库实现的路径。
  • 使用 -H 标志再次运行。即 g++ -H test.cpp -o gcd -g -Wall -Werror -pedantic-errors -fmessage-length=0 。这将显示预处理器尝试加载头文件。这可能表明正在发生什么。在我的 macbook 上,可以在此处找到 iostream。 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream
  • 你是用XCode的g++(其实是clang)还是自己安装的?
  • 我已经 brew 安装了 gcc-5 并创建了符号链接。
  • 不确定,但我认为 brew gcc-5 不安装头文件,只是编译器和指向 OSX 头文件的链接,因此您需要安装 xode 及其工具,因为在 osx 下默认不提供头文件.

标签: c++ macos gcc g++


【解决方案1】:

您需要确保已安装 Xcode 命令行工具。从终端运行:

xcode-select --install

更多信息:http://tips.tutorialhorizon.com/2015/10/01/xcrun-error-invalid-active-developer-path-library-developer-commandline-tools-missing-xcrun/

每当我想弄清楚编译器在 Xcode 中做了什么时,我总是会去看 Xcode 使用的命令是什么。在您的项目中,转到报告导航器(应该在左侧,最右侧的选项卡上):

然后找到您有兴趣进一步查看的文件所在的行:

点击最右边的一小叠煎饼展开:

【讨论】:

  • 嗯,好的,我到了那个菜单,但我不知道我在看什么,以了解为什么它在 Xcode 而不是终端中工作。
猜你喜欢
  • 2016-02-08
  • 1970-01-01
  • 2017-09-30
  • 2014-12-16
  • 2014-04-26
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多