使用 GCC 编译时"cmath: No such file or directory"



我用 Linux ubuntu 编写了简单的程序,当我使用 g++ 时没有错误,但是当我使用 gcc 时,我看到此错误:

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

注意:"事实上,我在编译软件包时看到了这个错误,我认为它可能与未设置为linux环境的gcc库有关,所以我编写了简单的程序来清楚地确定错误和依赖关系!所以程序应该使用 gcc 编译,这样我就可以克服主要问题。我知道我可以使用math.h而不是cmath,但是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 路径:

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 写了主要问题

请帮助我
我不知道该怎么办。
谢谢

一些基础知识:

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

两者都是根据需要调用编译器的驱动程序。

消除您的疑虑:

GCC的问题在于,默认情况下,它不会像G++那样链接 std C++库中的链接。 GCC只是一个前端。实际的编译器是cc1plus. 因此,在编译C++文件时始终建议使用 G++。如果您知道链接它们的确切参数,则 GCCG++ 的结果可能相同。您可能会发现此链接很有帮助。

但是,如果您仍然想使用 GCC ,请在命令末尾与链接器选项-lstdc++一起使用。默认情况下,当您使用 G++ 时会添加此链接器选项。您可以通过使用GCC -###选项编译代码来验证这一点,它将显示缺少-lstdc++选项。

使用 g

++ 而不是 gcc 编译C++源文件。

相关内容

最新更新