Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[g++] voir meme gcc ne veux pas compiler mon prog (résolu)
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index French
View previous topic :: View next topic  
Author Message
loopx
Advocate
Advocate


Joined: 01 Apr 2005
Posts: 2787
Location: Belgium / Liège

PostPosted: Tue Nov 01, 2005 7:26 pm    Post subject: [g++] voir meme gcc ne veux pas compiler mon prog (résolu) Reply with quote

Bonsoir, j'aimerai bien me lancer dans le C++... devrais déjà mais bon...

Alors, je me suis ouvert 3 consoles, utilisé vim comme un grand, et taper quelques ligne... J'essaye de faire un arbre binaire (on s'en fou mais bon...). Le plus simple des programmes ne veux pas compilé, et le message d'erreur veux rien dire pour moi, alors je vous demande de l'aide. Voici ou j'en suis arrivé (tout est dans le meme repertoire):

Code:

main.cpp:
#include <iostream.h>
#include "BTree.h"
#include <stdlib.h>

int main() {
        cout << "Hello World" << endl;

        cout << "Instanciation d'un BTree" << endl;

        BTree boulet(5);

        return 0;
}



BTree.h:
#include <iostream.h>
#include "BTree.h"
#include <stdlib.h>

int main() {
        cout << "Hello World" << endl;

        cout << "Instanciation d'un BTree" << endl;

        BTree boulet(5);

//cout << "Valeur dans mon arbre: " << boulet.getValeur();


//      cout << "Fin..." << endl;
        return 0;
}

loopx@loopx-nb ~/workspace/2Tree $ cat BTree.h
#include <iostream.h>

class BTree {
        private:
                int valeur;
                BTree *sag;
                BTree *sad;
        public:
                BTree(int);
                void setValeur(int);
                int getValeur();
};



BTree.cpp:
//implementation
#include "BTree.h"

BTree::BTree(int V)     {       valeur=V; sag=NULL; sad=NULL;   }

void BTree::setValeur(int V) {  valeur=V;       }
int BTree::getValeur()  {       return valeur;  }




makefile:
CC=g++
TOUT=main

all: $(TOUT)

main: main.o BTree.o
        @echo "compilation de tout ltralala"
        $(CC) -o main main.o BTree.o

main.o: main.cpp
        @echo "compilation de main.o"
        $(CC) -o main.o main.cpp

BTree.o: BTree.cpp
        @echo "compilation de BTree.o"
        $(CC) -o BTree.o BTree.cpp


J'ai fais un makefile (qui devrais fonctionner), j'ai 1 class BTree et un fichier pour le main....

Si j'utilise g++:
Code:

loopx@loopx-nb ~/workspace/2Tree $ make
compilation de main.o
g++ -o main.o main.cpp
In file included from /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/iostream.h:31,
                 from main.cpp:1:
/usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
/tmp/ccpzkPCA.o: In function `main':
main.cpp:(.text+0x66): undefined reference to `BTree::BTree[in-charge](int)'
collect2: ld returned 1 exit status
make: *** [main.o] Error 1
loopx@loopx-nb ~/workspace/2Tree $ make
compilation de main.o
g++ -o main.o main.cpp
In file included from /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/iostream.h:31,
                 from main.cpp:1:
/usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
/tmp/ccX8rWSF.o: In function `main':
main.cpp:(.text+0x66): undefined reference to `BTree::BTree[in-charge](int)'
collect2: ld returned 1 exit status
make: *** [main.o] Error 1

En gros, je sais pas du tout ce qu'il me veut :(

Et avec du gcc:
Code:

loopx@loopx-nb ~/workspace/2Tree $ make
compilation de main.o
gcc -o main.o main.cpp
In file included from /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/iostream.h:31,
                 from main.cpp:1:
/usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
/tmp/ccIhrKLh.o: In function `main':
main.cpp:(.text+0x14): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
main.cpp:(.text+0x21): undefined reference to `std::cout'
main.cpp:(.text+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.cpp:(.text+0x2f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
main.cpp:(.text+0x3a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
main.cpp:(.text+0x47): undefined reference to `std::cout'
main.cpp:(.text+0x4c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.cpp:(.text+0x55): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
main.cpp:(.text+0x66): undefined reference to `BTree::BTree[in-charge](int)'
/tmp/ccIhrKLh.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x92): undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccIhrKLh.o: In function `__tcf_0':
main.cpp:(.text+0xc1): undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccIhrKLh.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
make: *** [main.o] Error 1

J'ai peur!!..

Ce qui me fais super ch***, c'est que je sais pas d'ou il le sort le "[in-charge]"... Alors, pour moi, c'est clairement le compilateur qui m'en veut (quoique, il compile nikel tout ce qu'il faut pour ma gentoo) alors serais-je devenu fou ?

Help plz :oops:
_________________
Mon MediaWiki perso : http://pix-mania.dyndns.org


Last edited by loopx on Tue Nov 01, 2005 8:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
widan
Veteran
Veteran


Joined: 07 Jun 2005
Posts: 1512
Location: Paris, France

PostPosted: Tue Nov 01, 2005 7:48 pm    Post subject: Re: [g++] voir meme gcc ne veux pas compiler mon prog Reply with quote

Code:
main.o: main.cpp
        @echo "compilation de main.o"
        $(CC) -o main.o main.cpp

Il faut mettre un "-c" pour dire à gcc/g++ de faire juste la compilation, et pas le linkage:
Code:
$(CC) -c -o main.o main.cpp

Code:
In file included from /usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/iostream.h:31,
                 from main.cpp:1:
/usr/lib/gcc-lib/i386-pc-linux-gnu/3.3.6/include/g++-v3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.

Normalement pour les include standards C++, on ne met pas le ".h":
Code:
#include <iostream>

loopx wrote:
Et avec du gcc:
Code:
... une horreur ...

Il vaut mieux éviter gcc pour le C++. En fait gcc sait compiler du C++ (il essaye de deviner en fonction de l'extension du fichier...), mais absolument pas linker le programme résultant. Pour des programmes simples, on peut aider en rajoutant "-lstdc++", mais c'est pas sûr que ça soit suffisant dans tous les cas. Donc: utiliser le bon compilateur (et ne pas linker avec ld directement non plus - pour la même raison).
Back to top
View user's profile Send private message
kernelsensei
Bodhisattva
Bodhisattva


Joined: 22 Feb 2004
Posts: 5619
Location: Woustviller/Moselle/FRANCE (49.07°N;7.02°E)

PostPosted: Tue Nov 01, 2005 7:52 pm    Post subject: Reply with quote

euh, tu t'es pas trompé avec tes .h et tes .cpp ?

il faut aussi mettre le using namespace std;

EDIT:
@widan: il me semble qu'un simple g++ mesfonctions1.cpp mesfonctions2.cpp main.cpp -o machin passe aussi.
_________________
$ ruby -e'puts " .:@BFegiklnorst".unpack("x4ax7aaX6ax5aX15ax4aax6aaX7ax2aX5aX8 \
axaX3ax8aX4ax6aX3aX6ax3ax3aX9ax4ax2aX9axaX6ax3aX2ax4ax3aX4aXaX12ax10aaX7a").join'
Back to top
View user's profile Send private message
widan
Veteran
Veteran


Joined: 07 Jun 2005
Posts: 1512
Location: Paris, France

PostPosted: Tue Nov 01, 2005 7:58 pm    Post subject: Reply with quote

kernel_sensei wrote:
@widan: il me semble qu'un simple g++ mesfonctions1.cpp mesfonctions2.cpp main.cpp -o machin passe aussi.

Oui, ça marche aussi, mais apparemment dans son Makefile il veut d'abord compiler chaque fichier séparément, puis linker les deux fichiers objets ensemble. Toi tu fais les deux en même temps, et là effectivement il y a pas besoin de "-c" (mais ça devient vite moins intéressant quand le programme grossit et que le temps de compilation s'allonge...).
Back to top
View user's profile Send private message
loopx
Advocate
Advocate


Joined: 01 Apr 2005
Posts: 2787
Location: Belgium / Liège

PostPosted: Tue Nov 01, 2005 8:15 pm    Post subject: Reply with quote

kernel_sensei wrote:
euh, tu t'es pas trompé avec tes .h et tes .cpp ?

il faut aussi mettre le using namespace std;


Qu'est ce qu'ils ont mes .h et .cpp ??? C'est bien fait comme ca .... (.h pour ... le prototype ? et .cpp pour l'implémentation...).

Ca sert à quoi le truc using namspace std ? Et je le met juste dans le main ???
_________________
Mon MediaWiki perso : http://pix-mania.dyndns.org
Back to top
View user's profile Send private message
loopx
Advocate
Advocate


Joined: 01 Apr 2005
Posts: 2787
Location: Belgium / Liège

PostPosted: Tue Nov 01, 2005 8:19 pm    Post subject: Reply with quote

Nikel, ca fonctionne. J'avais mal compris comment on faisais pour avoir un objet... (je l'avais vu quelque part en plus, le -c). Merci pour les conseils, juste un dernier problème: quand je vire le .h de iostream, il me fait cette erreur (pour le main) :

Code:

compilation de main.o
g++ -c -o main.o main.cpp
main.cpp: In function `int main()':
main.cpp:6: error: `cout' undeclared (first use this function)
main.cpp:6: error: (Each undeclared identifier is reported only once for each
   function it appears in.)
main.cpp:6: error: `endl' undeclared (first use this function)
make: *** [main.o] Error 1


Si je remet le .h, il passe tout en mettant le message avec <X.h> gna gna <X> ... bla bla ... Alors la ... comprend plus :|

Par contre, du coté de la class BTree (dans le fichier .h), si je vire le .h de iostream.h, il ne pose pas de problème :?: :!: :idea: :arrow:
_________________
Mon MediaWiki perso : http://pix-mania.dyndns.org
Back to top
View user's profile Send private message
widan
Veteran
Veteran


Joined: 07 Jun 2005
Posts: 1512
Location: Paris, France

PostPosted: Tue Nov 01, 2005 8:23 pm    Post subject: Reply with quote

Soit tu remplaces "cout" par "std::cout" (idem pour "endl"), soit tu mets "using namespace std;" juste après les includes.
Back to top
View user's profile Send private message
loopx
Advocate
Advocate


Joined: 01 Apr 2005
Posts: 2787
Location: Belgium / Liège

PostPosted: Tue Nov 01, 2005 8:31 pm    Post subject: Reply with quote

ah je comprend...

merci ;)
_________________
Mon MediaWiki perso : http://pix-mania.dyndns.org
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index French All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum