gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 00:49:53 29 4
gpt4 key购买 nike

我在 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++ 的结果可能相同。你可能会发现这个 link有帮助。

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

关于linux - 使用 GCC 编译时为 "cmath: No such file or directory",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17147170/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com