Msgstr "cmath: no such file or directory" when compiling with GCC

I wrote a simple program in linux ubuntu , when I use g ++, there is no error, but when I use gcc , I see this error:

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

Note: "in fact, I see this error when compiling the package, I thought it could be due to a gcc library that is not configured for linux, so I wrote a simple program to clearly identify the error and whitout dependency!" therefore, the program must compile with gcc so that I can deal with the main problem. As I understand it, I can use math.h instead of cmath, but packege used cmath! this is a simple program:

  /*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; } 

here is 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 

and after installing gcc-4.8, I followed this instruction:

 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 

to make gcc-4.8 my default gcc.

now

 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. 

I actually wrote the main problem at https://askubuntu.com/questions/309195/cmath-no-such-file-or-directory-include-cmath

Please help me
I do not know what to do.
thanks

+6
source share
2 answers

Some basics ::

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

Both are drivers that are called by compilers if necessary.

Resolving your doubts ::

The problem with GCC is that by default it does not refer to std C ++ libraries , as G++ does. GCC is just an interface. The actual cc1plus. compiler cc1plus. Therefore, it is always recommended to use G++ when compiling files in C ++. The result may be the same with GCC and G++ if you know the exact arguments for linking them. You can find this link.

But if you still want to use GCC , use it with the -lstdc++ linker-layout at the end of the command. This linker option is added by default when using G++ . You can verify this by compiling your code using GCC with the -### parameter, and it will show you that the -lstdc++ parameter is missing.

+3
source

Compile the C ++ source files with g ++, not gcc.

+2
source

Source: https://habr.com/ru/post/947467/


All Articles