2013-06-17 12 views
6

でコンパイルしたとき、私は、私はg ++の使用時にエラーがない、LinuxのUbuntuので簡単なプログラムを書いたが、私はgccのを使用するとき、私はこのエラーを参照してください。「cmath:そのようなファイルやディレクトリはありませんが、」GCC

test.c:1:17: fatal error: cmath: No such file or directory #include <cmath> 

注:実際には、このエラーがパッケージをコンパイルするのを見ると、linux環境に設定されていないgccライブラリに関係している可能性があるので、エラーを明確に判断する簡単なプログラムを書きました。ウィットアウト依存! プログラムはgccでコンパイルする必要があります。 私はcmathの代わりにmath.hを使うことができますが、packegeはcmathを使いました! これは単純なプログラムです。ここ

/*test.c*/ 
#include <cmath> 
#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
using namespace std; 

int main(){ 
     double sinx = sin(3.14/3); 
cout<< "sinx= " << sinx; 

return 0; 
} 

がcmath pathesです:

roo[email protected]:/# find -name cmath 
./opt/root5.32.00/cint/cint/include/cmath 
./app/gcc/4.8.0/include/c++/4.8.0/ext/cmath 
./app/gcc/4.8.0/include/c++/4.8.0/cmath 
./app/gcc/4.8.0/include/c++/4.8.0/tr1/cmath 
./usr/include/boost/compatibility/cpp_c_headers/cmath 
./usr/include/boost/tr1/tr1/cmath 
./usr/include/c++/4.5/cmath 
./usr/include/c++/4.5/tr1_impl/cmath 
./usr/include/c++/4.5/tr1/cmath 
./usr/include/c++/4.6/cmath 
./usr/include/c++/4.6/tr1/cmath 
./usr/share/gccxml-0.9/GCC/2.95/cmath 
./gcc-build/gcc-4.8.0/stage1-i686-pc-linux-gnu/libstdc++-v3/include/ext/cmath 
./gcc-build/gcc-4.8.0/stage1-i686-pc-linux-gnu/libstdc++-v3/include/cmath 
./gcc-build/gcc-4.8.0/stage1-i686-pc-linux-gnu/libstdc++-v3/include/tr1/cmath 
./gcc-build/gcc-4.8.0/i686-pc-linux-gnu/libstdc++-v3/include/ext/cmath 
./gcc-build/gcc-4.8.0/i686-pc-linux-gnu/libstdc++-v3/include/cmath 
./gcc-build/gcc-4.8.0/i686-pc-linux-gnu/libstdc++-v3/include/tr1/cmath 
./gcc-build/gcc-4.8.0/libstdc++-v3/include/ext/cmath 
./gcc-build/gcc-4.8.0/libstdc++-v3/include/c/cmath 
./gcc-build/gcc-4.8.0/libstdc++-v3/include/c_global/cmath 
./gcc-build/gcc-4.8.0/libstdc++-v3/include/c_std/cmath 
./gcc-build/gcc-4.8.0/libstdc++-v3/include/tr1/cmath 
./gcc-build/gcc-4.8.0/libstdc++-v3/testsuite/26_numerics/headers/cmath 
./gcc-build/gcc-4.8.0/libstdc++-v3/testsuite/tr1/8_c_compatibility/cmath 
./gcc-build/gcc-4.8.0/prev-i686-pc-linux-gnu/libstdc++-v3/include/ext/cmath 
./gcc-build/gcc-4.8.0/prev-i686-pc-linux-gnu/libstdc++-v3/include/cmath 
./gcc-build/gcc-4.8.0/prev-i686-pc-linux-gnu/libstdc++-v3/include/tr1/cmath 

とgcc-4.8をインストールした後、私はこの命令をした:

[email protected]:~/Desktop# update-alternatives --install /usr/bin/gcc gcc /app/gcc/4.8.0/bin/gcc 40 --slave /usr/bin/g++ g++ /app/gcc/4.8.0/bin/g++ 

[email protected]:~/Desktop#update-alternatives --install /usr/bin/gcc gcc /app/gcc/4.8.0/bin/gcc 60 --slave /usr/bin/g++ g++ /app/gcc/4.8.0/bin/g++ 
[email protected]:~/Desktop# update-alternatives --config gcc 

のgcc-4.8の私を作るためにデフォルトのgcc。今

[email protected]:~/Desktop# gcc --version 
gcc (GCC) 4.8.0 
Copyright (C) 2013 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 

実際の問題として、私は私が何をすべきか知っている `tを私
助けてくださいhttps://askubuntu.com/questions/309195/cmath-no-such-file-or-directory-include-cmath

で主な問題を書きました。
ありがとう

+0

+1よくある質問とエラーをはっきりと判断するためのコードを簡略化します。 – Abhineet

+0

cmathはC++用ですが、Cでmath.hを使用してください。ファイルが.cで終わっていても、明らかにC++ファイルであれば、エンディングを変更してください。 – stdcall

答えて

2

g ++ではなくg ++でC++ソースファイルをコンパイルします。

3

いくつかの基本::

GCC:: GNU Compiler Collection 
G++:: GNU C++ Compiler 

の両方が必要に応じてコンパイラを呼び出すドライバです。

あなたの疑問をクリア::

GCCの問題は、それがないG++としてデフォルトでのSTD C++ライブラリでないリンクをしていることです。 GCCはフロントエンドに過ぎません。実際のコンパイラはcc1plus.です。したがって、C++ファイルをコンパイルするときは常にG++を使用することをお勧めします。それらをリンクするための正確な引数がわかっている場合、結果はGCCG++と同じになります。 linkが役に立ちます。

ただし、まだGCCを使用する場合は、コマンドの最後にリンカーオプション-lstdc++と一緒に使用してください。 G++を使用すると、このリンカーオプションがデフォルトで追加されます。 GCC-###オプションを使用してコードをコンパイルして確認すると、-lstdc++オプションが見つからないことがわかります。

+0

オプション '-lstdC++'でgccを使いましたが、うまくいきませんでした!エラーは残りました。私はこのコマンドを書きました: 'gcc test.c -lstdC++' –

+0

あなたが使ったコマンドを投稿できますか?また、g ++に-vオプションを指定すると、すべてのコマンドとオプションが表示されます。そこから正しいものを推論することができます。 – Abhineet

+0

'gcc test.c -lstdC++' 'root @ geant4:〜/ Desktop#g ++ -v' 組み込みの仕様を使用しています。 COLLECT_GCC = G ++ COLLECT_LTO_WRAPPER = /アプリ/ GCC/4.8.0/libexecに/ GCC/i686の-pc-linux-gnuのよう/ 4.8.0/LTO-ラッパー 対象:を使用して構成のi686-pc-linux-gnuのよう :。/configure --prefix =/app/gcc/4.8.0 スレッドモデル:posix gccバージョン4.8.0(GCC) –

関連する問題