2012-02-15 23 views
5

Ho provato a creare un programma semplice nel terminale.Impossibile compilare un semplice programma C++ in Ubuntu

#include <stdio.h> 
#include <stdlib.h> 
int main() 
{ 
     printf("TESTING"); 
     return 1; 
} 

ho corse g ++ -o prova test.cpp

Gli errori:

/usr/include/features.h:323:26: error: bits/predefs.h: No such file or directory 
/usr/include/features.h:356:25: error: sys/cdefs.h: No such file or directory 
/usr/include/features.h:388:23: error: gnu/stubs.h: No such file or directory 
In file included from test.cpp:2: 
/usr/include/stdlib.h:42:29: error: bits/waitflags.h: No such file or directory 
/usr/include/stdlib.h:43:30: error: bits/waitstatus.h: No such file or directory 
/usr/include/stdlib.h:320:49: error: sys/types.h: No such file or directory 
In file included from test.cpp:2: 
/usr/include/stdlib.h:35: error: ‘__BEGIN_DECLS’ does not name a type 
/usr/include/stdlib.h:102: error: expected constructor, destructor, or type conversion before ‘;’ token 
/usr/include/stdlib.h:113: error: ‘__END_NAMESPACE_STD’ does not name a type 
/usr/include/stdlib.h:122: error: expected constructor, destructor, or type conversion before ‘;’ token 
/usr/include/stdlib.h:140: error: expected constructor, destructor, or type conversion before ‘extern’ 
/usr/include/stdlib.h:145: error: expected constructor, destructor, or type conversion before ‘extern’ 
/usr/include/stdlib.h:149: error: expected initializer before ‘__THROW’ 
/usr/include/stdlib.h:152: error: expected initializer before ‘__THROW’ 
/usr/include/stdlib.h:153: error: ‘__END_NAMESPACE_STD’ does not name a type 
/usr/include/stdlib.h:160: error: ‘__END_NAMESPACE_C99’ does not name a type 
/usr/include/stdlib.h:168: error: ‘__END_NAMESPACE_STD’ does not name a type 

La lista continua in questo modo. Spero che qualcuno possa far notare ciò che non ho fatto per far funzionare questo.

+0

Cosa offre 'g ++ --verbose -o test test.cpp'? – genpfault

+1

vedi se http://ubuntuforums.org/showthread.php?t=1877944 aiuta –

+0

Forse ho risolto il problema. Ho controllato l'output dettagliato e ho deciso di semplificare il percorso. Ho cambiato solo /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin Il programma viene compilato ora, ma non emette alcunché quando viene eseguito . È normale? –

risposta

2

SOLUZIONE: il mio percorso era vuoto a causa di alcuni tentativi precedenti di farlo funzionare. Ho creato un percorso pulito utilizzando:

export PATH= /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 

Il mio problema dopo la compilazione è che il programma non avrebbe mostrato alcun risultato. Ciò era dovuto al fatto che come nuovo utente Linux non mi ero reso conto di aver bisogno di chiamare un programma con ./ di fronte. Questo può essere impostato anche nel percorso chiamando:

export PATH: $PATH:./ 
+1

[Avere '.' nel tuo PERCORSO può essere piuttosto pericoloso.] (Https: //unix.stackexchange.it/questions/65700/is-it-safe-to-add-to-my-path-how-come) –

4

Il tuo codice funziona per me con la stessa piattaforma.

I messaggi di errore sembrano errori C. Forse servirà l'uso delle intestazioni C++.

#include <cstdio> 
#include <cstdlib> 

int main(int argc, char *argv[]) { 
    printf("TESTING"); 
    return 0; 
} 

Potresti anche avere alcuni alias strani. A volte le persone configurano gcc come alias per g ++ in modo errato.

[email protected]:~$ set | grep g++ 

[email protected]:~$ alias grep 
alias grep='grep --color=auto' 

[email protected]:~$ alias g++ 
bash: alias: g++: not found 

[email protected]:~$ which g++ 
/usr/bin/g++ 

[email protected]:~$ ll `which g++` 
lrwxrwxrwx 1 root root 7 2011-08-14 02:17 /usr/bin/g++ -> g++-4.6* 

[email protected]:~$ g++ --version 
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1 
Copyright (C) 2011 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. 

Questo è quanto ho installato il mio ambiente dev in Ubuntu:

sudo apt-get install build-essential 

Questo imposta tutte le librerie standard di C++ senza dover conoscere i dettagli Knitty grintoso.

+0

Grazie, l'ho capito anche se –

3

Avevo un problema molto simile a questo. Nel mio caso il problema era che avevo alcuni file di intestazione danneggiati, come evidenziato, cercando di vederle:

/usr/include/x86_64-linux-gnu/sys$ cat * | grep "Input/outpu error" 
cat: ioctl.h: Input/output error 
cat: types.h: Input/output error 

La soluzione per me è stato quello di eliminare questi file e poi reinstallare.

sudo apt-get purge libc6-dev 
sudo apt-get install libc6-dev 
+1

È anche possibile reinstallare i pacchetti usando 'aptitude reinstall'. – tmandry

+1

Se qualcuno non ha installato aptitude': anche sudo apt-get install --reinstall libc6-dev' funziona. – ElmoVanKielmo

+0

apt-get purge ha rimosso tutto ciò che avevo precedentemente installato. Passando attraverso il problema di doverli installare di nuovo tutti. – user3124361

Problemi correlati