注:xcodeとそのコマンドラインツールがインストールされています。Mac端末のコンパイルエラー
gcd.cpp:6:20: fatal error:
iostream
: No such file or directory
実行しているとき、私は、端末に取得エラーです:
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);
}
UPDATEコマンドを実行
:
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 – ccpgh
XCodeのg ++(実際にはclang)を使用していますか、自分でインストールしていますか? – molbdnilo