Mac终端编译错误

时间:2017-01-24 14:02:08

标签: c++ macos gcc g++

注意:我确实安装了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

1 个答案:

答案 0 :(得分:1)

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

.style

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

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

enter image description here

然后找到您感兴趣的文件行:

enter image description here

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

enter image description here