Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"cmath: No such file or directory" when compiled with GCC

Tags:

linux

gcc

ubuntu

I wrote the 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 : "as a matter of fact I see this error in compiling the package, I thought it might be related to gcc library which is not set to linux environment, so I wrote the simple program to determine the error clearly and whitout dependency!" so the program should compile with gcc so that I can over come the main problem. I khow that I can use math.h instead of cmath, but the packege used the cmath! this is the 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 did 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.

as a matter of fact I wrote the main problem in https://askubuntu.com/questions/309195/cmath-no-such-file-or-directory-include-cmath

please help me
I don`t know what to do.
thanks

like image 845
alireza yousefi Avatar asked Jun 17 '13 12:06

alireza yousefi


2 Answers

Some basics::

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

Both are drivers which calls the compilers as needed.

Clearing your doubt::

The problem with GCC is that it doesn't links in the std C++ libraries by default as G++ does. GCC is just a front-end. The actual compiler is cc1plus. So it is always advisable to use G++ when compiling C++ files. The result can be same with both GCC and G++ if you do know the exact arguments to link them. You may find this link helpful.

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

like image 62
Abhineet Avatar answered Sep 28 '22 04:09

Abhineet


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

like image 26
tkroman Avatar answered Sep 28 '22 06:09

tkroman