【问题标题】:"cmath: No such file or directory" when compiled with GCC使用 GCC 编译时“cmath:没有这样的文件或目录”
【发布时间】:2013-06-17 12:10:04
【问题描述】:

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

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

注意:“事实上我在编译包时看到了这个错误,我认为它可能与没有设置为linux环境的gcc库有关,所以我编写了一个简单的程序来清楚地确定错误并且whitout依赖!” 所以程序应该用 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

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

【问题讨论】:

  • +1 表示好问题并简化代码以清楚地确定错误。
  • cmath 用于 C++,使用 math.h 用于 C,如果文件以 .c 结尾但很明显是 C++ 文件,请更改结尾。

标签: linux ubuntu gcc


【解决方案1】:

一些基础知识::

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

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

清除你的疑问::

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

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

【讨论】:

  • 我用 gcc 和 -lstdc++ 选项但它没有t work! the error remained.I wrote this command: gcc test.c -lstdc++`
  • 你能把你用过的命令贴出来吗?此外,尝试使用带有 -v 选项的 g++,它会显示所有命令和选项。你可以从那里推断出正确的。
  • 将文件重命名为“test.cpp”。 GCC 通过查看文件的扩展名来调用相应的编译器。
  • @alirezayousefi 构建在编译阶段失败,而不是链接阶段,所以添加-lstdc++ 不会发生。问题是,g++ 还有其他包含路径作为 gcc - 并且“cmath”是 c++ 标头仅在程序编译为 C++ 时才被搜索
  • 如果您的主要问题是这个,那么您可能应该在那里等待解决方案,并且不应该开始到处发布您的问题。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2016-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 2021-10-17
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多