【发布时间】: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 下默认不提供头文件.