关于linux:使用GCC编译时,“ cmath:没有这样的文件或目录”

“cmath: No such file or directory” when compiled with GCC

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

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

注意:"事实上,我在编译软件包时看到此错误,我认为它可能与未设置为linux环境的gcc库有关,所以我编写了简单的程序来清楚地确定错误并确定依赖性!"
因此程序应使用gcc进行编译,这样我就可以克服主要问题。
我知道我可以使用math.h代替cmath,但是packege使用cmath!
这是简单的程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
   /*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路径:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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之后,我做了以下指示:

1
2
3
4
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。

现在

1
2
3
4
5
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中写了主要问题

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


一些基本知识:

1
2
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 ++源文件。