2013-06-17 8 views
6

Napisałem prosty program w Linux Ubuntu, gdy używam g ++ nie ma błędu, ale kiedy używam gcc widzę ten błąd:„cmath: Nie ma takiego pliku lub katalogu”, gdy skompilowany z GCC

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

Uwaga: "w rzeczywistości widzę ten błąd podczas kompilowania pakietu, myślałem, że może to być związane z biblioteką gcc, która nie jest ustawiona na środowisko linuxowe, więc napisałem prosty program, aby jasno określić błąd i uzależnienie od białek! " , więc program powinien skompilować się z gcc, tak aby mogłem przezwyciężyć główny problem. I khow, że mogę użyć math.h zamiast cmath, ale packege użył cmath! jest to prosty 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; 
} 

Oto cmath pathes:

[email protected]:/# 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 

i po zainstalowaniu gcc-4.8 zrobiłem tej instrukcji:

[email protected]:~/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++ 

[email protected]:~/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++ 
[email protected]:~/Desktop# update-alternatives --config gcc 

aby gcc-4.8 Mojej domyślna gcc.

teraz

[email protected]:~/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. 

jako rzeczywistości napisałem główny problem w https://askubuntu.com/questions/309195/cmath-no-such-file-or-directory-include-cmath

pomóż mi
nie wiem co robić.
dziękuje

+0

+1 za dobre pytanie i uproszczenie kodu w celu wyraźnego określenia błędu. – Abhineet

+0

cmath jest dla C++, użyj math.h dla C, jeśli plik kończy się na .c ale jest wyraźnie i C++, zmień zakończenie. – stdcall

Odpowiedz

2

Skompiluj pliki źródłowe C++ za pomocą g ++, a nie gcc.

3

Pewne podstawy ::

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

Oba są sterowniki, które wywołuje kompilatorów, ile potrzeba.

Usuwanie swoje wątpliwości ::

Problem z GCC jest to, że nie ma odnośników w std bibliotek C++ domyślnie jako G++ robi. GCC to tylko front-end. Rzeczywisty kompilator to cc1plus.. Podczas kompilowania plików C++ zawsze zaleca się używanie G++. Wynik może być taki sam zarówno dla GCC i G++, jeśli znasz dokładne argumenty, aby je połączyć. Pomocna może okazać się ta link.

Ale jeśli nadal chcesz używać GCC, użyj go z opcją łącznika -lstdc++ na końcu polecenia. Ta opcja linkera jest dodawana domyślnie podczas korzystania z G++. Możesz to sprawdzić, kompilując kod przy użyciu opcji GCC z opcją -###, która pokaże, że brakuje opcji -lstdc++.

+0

Użyłem gcc z opcją '-lstdC++', ale to nie zadziałało! błąd pozostał. Napisałem to polecenie: 'gcc test.c -lstdC++' –

+0

Czy umieścisz polecenie, którego użyłeś? Spróbuj także g ++ z opcją -v, pokaże ci wszystkie polecenia i opcje. Możesz stąd wydedukować poprawne. – Abhineet

+0

'gcc test.c -lstdC++' 'root @ geant4: ~/Pulpit # g ++ -v' Korzystanie z wbudowanych specyfikacji. COLLECT_GCC = g ++ COLLECT_LTO_WRAPPER =/APP/GCC/4.8.0/libexec/gcc/i686-PC-Linux-gnu/4.8.0/Ito, owijka docelowa: i686-PC-Linux-gnu funkcji:./configure --prefix =/app/gcc/4.8.0 Model gwintu: posix gcc wersja 4.8.0 (GCC) –

Powiązane problemy