我在linux ubuntu中编写了简单的程序,当我使用g ++时没有错误但是当我使用gcc时我看到了这个错误:
test.c:1:17: fatal error: cmath: No such file or directory #include <cmath>
Run Code Online (Sandbox Code Playgroud)
注意:"事实上我在编译包时看到了这个错误,我认为它可能与gcc库有关,它没有设置为linux环境,所以我编写了简单的程序来明确地确定错误和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;
}
Run Code Online (Sandbox Code Playgroud)
这是cmath pathes:
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
Run Code Online (Sandbox Code Playgroud)
安装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
Run Code Online (Sandbox Code Playgroud)
使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.
Run Code Online (Sandbox Code Playgroud)
事实上,我在https://askubuntu.com/questions/309195/cmath-no-such-file-or-directory-include-cmath中写了主要问题
请帮帮我,
我不知道该怎么做.
谢谢
一些基本知识:
GCC:: GNU Compiler Collection
G++:: GNU C++ Compiler
Run Code Online (Sandbox Code Playgroud)
两者都是根据需要调用编译器的驱动程序。
消除您的疑问::
问题在于默认情况下GCC它不链接std C ++库G++。GCC只是一个前端。实际的编译器为。cc1plus.因此,建议始终G++在编译C ++文件时使用。两者的结果可能相同,GCC并且G++如果您确实知道链接它们的确切参数。您可能会发现此链接很有帮助。
但是,如果仍然要使用GCC,请-lstdc++在命令末尾将它与linker-option一起使用。使用时,默认情况下会添加此链接器选项G++。您可以通过使用GCCwith -###选项编译代码来验证这一点,它将显示该-lstdc++选项丢失。