8

Linux ubuntuで簡単なプログラムを作成しました。g++ を使用するとエラーは発生しませんが、gccを使用すると次のエラーが表示されます。

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

注 : 「パッケージのコンパイル中にこのエラーが発生しました。Linux 環境に設定されていない gcc ライブラリに関連している可能性があると考えたので、エラーを明確かつ依存関係なく判別するための簡単なプログラムを作成しました!」主な問題を克服できるように、プログラムは gcc でコンパイルする必要があります。cmath の代わりに math.h を使用できることはわかっていますが、パッケージは 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パスがあります:

root@geant4:/# 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をインストールした後、私はこの指示をしました:

root@geant4:~/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++

root@geant4:~/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++
root@geant4:~/Desktop# update-alternatives --config gcc

gcc-4.8 をデフォルトの gcc にします。

root@geant4:~/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.

実際のところ、私はhttps://askubuntu.com/questions/309195/cmath-no-such-file-or-directory-include-cmathに主な問題を書きました


どうすればいいのかわからない私を助けてください。
ありがとう

4

2 に答える 2

6

いくつかの基本::

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

どちらも必要に応じてコンパイラを呼び出すドライバです。

疑問を解消する::

問題は、デフォルトでstd C++ ライブラリGCCにリンクしないことです。は単なるフロントエンドです。実際のコンパイラは、C++ ファイルをコンパイルするときに常に使用することをお勧めします。結果は両方で同じになる可能性があり、それらをリンクするための正確な引数がわかっている場合。このリンクが役立つ場合があります。G++GCCcc1plus.G++GCCG++

それでも を使用したい場合は、コマンドの最後にGCClinker-optionを付けて使用してください。-lstdc++を使用すると、このリンカー オプションがデフォルトで追加されますG++GCCwithオプションを使用してコードをコンパイルすることでこれを確認でき、オプションが欠落-###していることが示されます。-lstdc++

于 2013-06-17T12:27:26.253 に答える
3

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

于 2013-06-17T12:13:35.943 に答える