C++ errore in fase di link

Forum dedicato alla programmazione.

Moderatore: Staff

Regole del forum
1) Citare in modo preciso il linguaggio di programmazione usato.
2) Se possibile portare un esempio del risultato atteso.
3) Leggere attentamente le risposte ricevute.
4) Scrivere i messaggi con il colore di default, evitare altri colori.
5) Scrivere in Italiano o in Inglese, se possibile grammaticalmente corretto, evitate stili di scrittura poco chiari, quindi nessuna abbreviazione tipo telegramma o scrittura stile SMS o CHAT.
6) Appena registrati è consigliato presentarsi nel forum dedicato.

La non osservanza delle regole porta a provvedimenti di vari tipo da parte dello staff, in particolare la non osservanza della regola 5 porta alla cancellazione del post e alla segnalazione dell'utente. In caso di recidività l'utente rischia il ban temporaneo.
Avatar utente
N1cuz
Linux 2.x
Linux 2.x
Messaggi: 333
Iscritto il: lun 6 ott 2008, 0:41
Nome Cognome: Nicola Bartolomei
Slackware: 14.1
Kernel: 4.3.3
Desktop: xfce4
Località: Pieve a Nievole (PT)

C++ errore in fase di link

Messaggio da N1cuz »

Ho scritto un programmino in c++, sono riuscito a compilare correttamente tutti i file singolarmente, solo che al momento della creazione dell'eseguibile ottengo un errore in fase di link e non riesco a capire dove sia il problema, posto l'output:

Codice: Seleziona tutto

MyString.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
MyString.cpp:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
MyString.cpp:(.text+0x55): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
MyString.cpp:(.text+0x8e): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
MyString.cpp:(.text+0xd3): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
MyString.o: In function `__static_initialization_and_destruction_0(int, int)':
MyString.cpp:(.text+0x124): undefined reference to `std::ios_base::Init::Init()'
MyString.o: In function `__tcf_0':
MyString.cpp:(.text+0x1b3): undefined reference to `std::ios_base::Init::~Init()'
MyString.o: In function `MyString<char>::Add(char const&)':
MyString.cpp:(.text._ZN8MyStringIcE3AddERKc[MyString<char>::Add(char const&)]+0x26): undefined reference to `operator new[](unsigned int)'
MyString.cpp:(.text._ZN8MyStringIcE3AddERKc[MyString<char>::Add(char const&)]+0x74): undefined reference to `operator delete[](void*)'
MyString.cpp:(.text._ZN8MyStringIcE3AddERKc[MyString<char>::Add(char const&)]+0x95): undefined reference to `operator new[](unsigned int)'
MyString.o: In function `MyString<char>::MyString(char const*, char)':
MyString.cpp:(.text._ZN8MyStringIcEC1EPKcc[MyString<char>::MyString(char const*, char)]+0x29): undefined reference to `operator new[](unsigned int)'
MyString.o: In function `MyString<char>::Destroy()':
MyString.cpp:(.text._ZN8MyStringIcE7DestroyEv[MyString<char>::Destroy()]+0x1b): undefined reference to `operator delete[](void*)'
MyString.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
StringTest.o: In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
StringTest.cpp:(.text+0xe): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const'
StringTest.cpp:(.text+0x55): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
StringTest.cpp:(.text+0x8e): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
StringTest.cpp:(.text+0xd3): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const'
StringTest.o: In function `__static_initialization_and_destruction_0(int, int)':
StringTest.cpp:(.text+0x124): undefined reference to `std::ios_base::Init::Init()'
StringTest.o: In function `__tcf_0':
StringTest.cpp:(.text+0x1b3): undefined reference to `std::ios_base::Init::~Init()'
StringTest.o: In function `main':
StringTest.cpp:(.text+0x1d8): undefined reference to `MyString<char>::MyString(char)'
StringTest.cpp:(.text+0x1e9): undefined reference to `MyString<char>::MyString(char)'
StringTest.cpp:(.text+0x1fa): undefined reference to `MyString<char>::MyString(char)'
StringTest.cpp:(.text+0x220): undefined reference to `std::cout'
StringTest.cpp:(.text+0x225): 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*)'
StringTest.cpp:(.text+0x234): undefined reference to `std::cin'
StringTest.cpp:(.text+0x239): undefined reference to `std::basic_istream<char, std::char_traits<char> >& operator>><char>(std::basic_istream<char, std::char_traits<char> >&, MyString<char>&)'
StringTest.cpp:(.text+0x249): undefined reference to `std::cout'
StringTest.cpp:(.text+0x24e): 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*)'
StringTest.cpp:(.text+0x25d): undefined reference to `std::cin'
StringTest.cpp:(.text+0x262): undefined reference to `std::basic_istream<char, std::char_traits<char> >& operator>><char>(std::basic_istream<char, std::char_traits<char> >&, MyString<char>&)'
StringTest.cpp:(.text+0x272): undefined reference to `std::cout'
StringTest.cpp:(.text+0x277): 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*)'
StringTest.cpp:(.text+0x286): undefined reference to `std::cin'
StringTest.cpp:(.text+0x28b): undefined reference to `std::basic_istream<char, std::char_traits<char> >& operator>><char>(std::basic_istream<char, std::char_traits<char> >&, MyString<char>&)'
StringTest.cpp:(.text+0x29e): undefined reference to `MyString<char>::MyString(MyString<char> const&)'
StringTest.cpp:(.text+0x2af): undefined reference to `MyString<char>::GetAt(int) const'
StringTest.cpp:(.text+0x2c3): undefined reference to `MyString<char>::GetAt(int) const'
StringTest.cpp:(.text+0x2e5): undefined reference to `MyString<char>::GetAt(int) const'
StringTest.cpp:(.text+0x2f6): 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> >&)'
StringTest.cpp:(.text+0x2fb): undefined reference to `std::cout'
StringTest.cpp:(.text+0x300): 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> >&))'
StringTest.cpp:(.text+0x311): 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*)'
StringTest.cpp:(.text+0x323): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, MyString<char> const&)'
StringTest.cpp:(.text+0x334): 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*)'
StringTest.cpp:(.text+0x346): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, MyString<char> const&)'
StringTest.cpp:(.text+0x357): 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*)'
StringTest.cpp:(.text+0x369): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, MyString<char> const&)'
StringTest.cpp:(.text+0x37a): 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*)'
StringTest.cpp:(.text+0x38c): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, MyString<char> const&)'
StringTest.cpp:(.text+0x39d): 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*)'
StringTest.cpp:(.text+0x3ac): 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)'
StringTest.cpp:(.text+0x3bd): 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*)'
StringTest.cpp:(.text+0x3cc): 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)'
StringTest.cpp:(.text+0x3dd): 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*)'
StringTest.cpp:(.text+0x3ec): 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)'
StringTest.cpp:(.text+0x3fd): 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*)'
StringTest.cpp:(.text+0x40f): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, MyString<char> const&)'
StringTest.cpp:(.text+0x41a): 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> >&)'
StringTest.cpp:(.text+0x420): 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> >&))'
StringTest.cpp:(.text+0x433): undefined reference to `MyString<char>::operator=(MyString<char> const&)'
StringTest.cpp:(.text+0x442): undefined reference to `std::cout'
StringTest.cpp:(.text+0x447): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <char>(std::basic_ostream<char, std::char_traits<char> >&, MyString<char> const&)'
StringTest.cpp:(.text+0x452): 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> >&)'
StringTest.cpp:(.text+0x458): 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> >&))'
StringTest.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
... Ho capito che il compilatore non riesce ad eseguire il link, ma perchè???

zxzxasas
Linux 2.x
Linux 2.x
Messaggi: 397
Iscritto il: sab 15 nov 2008, 2:28
Nome Cognome: Pasquale
Slackware: current
Kernel: 2.6.35.7
Desktop: KDE 4.5.2

Re: C++ errore in fase di link

Messaggio da zxzxasas »

Ok io faccio la domanda...

Come compili il tutto?

Mi pare che il messaggio di gcc dica chiaramente che ci sono dei riferimenti non risolti, cosa che mi viene ancora di più da pensare visto che dici che riesci a creare i moduli oggetto dei singoli file.

zxzxasas
Linux 2.x
Linux 2.x
Messaggi: 397
Iscritto il: sab 15 nov 2008, 2:28
Nome Cognome: Pasquale
Slackware: current
Kernel: 2.6.35.7
Desktop: KDE 4.5.2

Re: C++ errore in fase di link

Messaggio da zxzxasas »

Visto che ci siamo aggiungo qualche dettaglio sicchè qualcuno che usa gcc, se sto sbagliando, mi possa correggere.
Se non sbaglio gcc vuole in ingresso prima i file che hanno riferimenti in altri file, in modo tale che quando il codice oggetto viene linkato ha già a disposizione la tabella dei simboli dell' altro file in modo da poter eseguire un' esportazione corretta.

Ma di questo non sono sicuro perchè io ho sempre usato i makefile.

Una cosa però potresti provarla.
Installati il code::block e prova a compilare da lì...se compila, il problema è nel modo in cui passi i file a gcc (visto che anche il code::block si appoggia su gcc ;))

Avatar utente
N1cuz
Linux 2.x
Linux 2.x
Messaggi: 333
Iscritto il: lun 6 ott 2008, 0:41
Nome Cognome: Nicola Bartolomei
Slackware: 14.1
Kernel: 4.3.3
Desktop: xfce4
Località: Pieve a Nievole (PT)

Re: C++ errore in fase di link

Messaggio da N1cuz »

... ho provato sia con un makefile, sia con i comandi:

Codice: Seleziona tutto

gcc -c nome_file1.cpp    // file che contiene i riferimenti non trovati...
gcc -c  nome_file2.cpp
gcc -o exec nome_file1.o nome_file2.o
con risultato identico...

Avatar utente
Mithenks`
Linux 0.x
Linux 0.x
Messaggi: 21
Iscritto il: dom 25 mag 2008, 12:54
Slackware: 12.1
Località: Salerno/Siena
Contatta:

Re: C++ errore in fase di link

Messaggio da Mithenks` »

Per compilare i programmi in C++ devi usare g++ al posto di gcc.

Prova e dovrebbe risolversi tutto...


Ciao!

Avatar utente
N1cuz
Linux 2.x
Linux 2.x
Messaggi: 333
Iscritto il: lun 6 ott 2008, 0:41
Nome Cognome: Nicola Bartolomei
Slackware: 14.1
Kernel: 4.3.3
Desktop: xfce4
Località: Pieve a Nievole (PT)

Re: C++ errore in fase di link

Messaggio da N1cuz »

Mithenks` ha scritto:Per compilare i programmi in C++ devi usare g++ al posto di gcc.

Prova e dovrebbe risolversi tutto...


Ciao!

Si, scusa nella fretta ho postato male, uso g++ non gcc, comunque ottengo l'errore di link...

Avatar utente
Blizzard
Master
Master
Messaggi: 1509
Iscritto il: mar 2 gen 2007, 22:53
Nome Cognome: Giovanni Santostefano
Slackware: 12.2
Kernel: 2.6.27.7-smp
Desktop: Fluxbox
Contatta:

Re: C++ errore in fase di link

Messaggio da Blizzard »

se usi string
#include <string> o string.h
se usi iostream
#include <iostream>
using namespace std; //altrimenti std::cin std::cout

posta la parte che interessa il preprocessore. Magari si capisce meglio dove può essere l'errore di link

Avatar utente
N1cuz
Linux 2.x
Linux 2.x
Messaggi: 333
Iscritto il: lun 6 ott 2008, 0:41
Nome Cognome: Nicola Bartolomei
Slackware: 14.1
Kernel: 4.3.3
Desktop: xfce4
Località: Pieve a Nievole (PT)

Re: C++ errore in fase di link

Messaggio da N1cuz »

Blizzard ha scritto:se usi string
#include <string> o string.h
se usi iostream
#include <iostream>
using namespace std; //altrimenti std::cin std::cout

posta la parte che interessa il preprocessore. Magari si capisce meglio dove può essere l'errore di link
... non ho usato string ne altre classi della libreria standard (escluso quelle che operano sugli stream), comunque la parte relativa al preprocessore di MyString.h:

Codice: Seleziona tutto

#ifndef _MyString_h
#define _MySrtring_h 1
#include <iostream>
#include "Exception.h"
...
...
MyString.cpp:

Codice: Seleziona tutto

#include "MyString.h"
...
...
StringTest.cpp:

Codice: Seleziona tutto

#include "MyString.h"
using namespace std;   //non ho provato ad includere <iostream> nel file principale, 
...                                //comunque non viene trovato alcun riferimento alle funzioni 
...                               //di "MyString.h" non solo agli operator<< e >>
Tutti i file sono nella stessa directory e comuque ho provato anche ad esplicitare i pat dei files nel Makefile, ma senza risultato...

Avatar utente
Blizzard
Master
Master
Messaggi: 1509
Iscritto il: mar 2 gen 2007, 22:53
Nome Cognome: Giovanni Santostefano
Slackware: 12.2
Kernel: 2.6.27.7-smp
Desktop: Fluxbox
Contatta:

Re: C++ errore in fase di link

Messaggio da Blizzard »

se in MyString.cpp hai utilizzato cin o cout per esempio senza std:: allora può essere quello l'errore.

Aggiungi a MyString.h
using namespace std;

e toglilo da tutti i files che includono MyString.h come ad esempio StringTest.cpp

fai questa prova

Avatar utente
Blizzard
Master
Master
Messaggi: 1509
Iscritto il: mar 2 gen 2007, 22:53
Nome Cognome: Giovanni Santostefano
Slackware: 12.2
Kernel: 2.6.27.7-smp
Desktop: Fluxbox
Contatta:

Re: C++ errore in fase di link

Messaggio da Blizzard »

#define _MySrtring_h 1
errore di battitura

comunque puoi omettere l'1

Avatar utente
N1cuz
Linux 2.x
Linux 2.x
Messaggi: 333
Iscritto il: lun 6 ott 2008, 0:41
Nome Cognome: Nicola Bartolomei
Slackware: 14.1
Kernel: 4.3.3
Desktop: xfce4
Località: Pieve a Nievole (PT)

Re: C++ errore in fase di link

Messaggio da N1cuz »

Blizzard ha scritto:#define _MySrtring_h 1
errore di battitura
Scusami, non so come ci sia finita la r, comunque nel file è giusto...
comunque puoi omettere l'1
lo so, ma è un abitudine che ho preso.....

Avatar utente
N1cuz
Linux 2.x
Linux 2.x
Messaggi: 333
Iscritto il: lun 6 ott 2008, 0:41
Nome Cognome: Nicola Bartolomei
Slackware: 14.1
Kernel: 4.3.3
Desktop: xfce4
Località: Pieve a Nievole (PT)

Re: C++ errore in fase di link

Messaggio da N1cuz »

... oh Blizzarde (mi permetto un pò di confidenza...), lo sai che forse sono un baccello?!....
chiedo conferma a te perchè non ne sono certo, ma visto che MyString (avendo preso spunto dalla std) è una classe templatizzata ed in StringTest l' ho testata con stringhe di caratteri, non è che il problema deriva dal fatto che in "MyString.h" (inclusa da StringTest) ci sono solo i prototipi?? Correggimi (e perdonami) se dico sciocchezze... (comunque una prova la faccio...)

Avatar utente
Blizzard
Master
Master
Messaggi: 1509
Iscritto il: mar 2 gen 2007, 22:53
Nome Cognome: Giovanni Santostefano
Slackware: 12.2
Kernel: 2.6.27.7-smp
Desktop: Fluxbox
Contatta:

Re: C++ errore in fase di link

Messaggio da Blizzard »

ahhhhhhhhhhhhhh...
se hai utilizzato i template e hai suddiviso il codice in .h (interfaccia) .cpp (implementazione) dovevi prendere dei particolari accorgimenti nelle firme delle funzioni.
Mi sono incasinato una volta con questo tipo di problema. Vedi un tutorial buono sui template C++ e troverai anche la corretta dichiarazione dei metodi nel file cpp

;)

Avatar utente
Blizzard
Master
Master
Messaggi: 1509
Iscritto il: mar 2 gen 2007, 22:53
Nome Cognome: Giovanni Santostefano
Slackware: 12.2
Kernel: 2.6.27.7-smp
Desktop: Fluxbox
Contatta:

Re: C++ errore in fase di link

Messaggio da Blizzard »


Avatar utente
N1cuz
Linux 2.x
Linux 2.x
Messaggi: 333
Iscritto il: lun 6 ott 2008, 0:41
Nome Cognome: Nicola Bartolomei
Slackware: 14.1
Kernel: 4.3.3
Desktop: xfce4
Località: Pieve a Nievole (PT)

Re: C++ errore in fase di link

Messaggio da N1cuz »

grazie mille per il link (cplusplus.com è pieno di tutorial utilissimi!)... avevo completamente trascurato i problemi annessi all' implementazione con template, comunque adesso la stringa riesco a renderla eseguibile, adesso però ho un altro problema, piuttosto strano, in fase di link con il "pezzo" di programma successivo, output:

Codice: Seleziona tutto

HashTest.o: In function `__tcf_3':
/home/nico/Finf_II/C++/Project/MyString.h:46: multiple definition of `Center::r'
HashTable.o:/home/nico/Finf_II/C++/Project/MyString.h:46: first defined here
HashTest.o: In function `__tcf_4':
/home/nico/Finf_II/C++/Project/MyString.h:62: multiple definition of `GuardWing::r'
HashTable.o:/home/nico/Finf_II/C++/Project/MyString.h:62: first defined here
HashTest.o: In function `__tcf_4':
/home/nico/Finf_II/C++/Project/MyString.h:254: multiple definition of `PlayMaker::p'
HashTable.o:/home/nico/Finf_II/C++/Project/MyString.h:116: first defined here
collect2: ld returned 1 exit status
make: *** [HashTest] Error 1
non riesco a capire come mai viene segnalata una definizione multipla dei membri statici delle classi PlayMaker, Center e GuardWing che sono definite all'interno delle loro classi e non in MyString.h da dove pare provenga la definizione multipla... la realizzazione di sto programmetto è più travagliata di un parto!!!

Rispondi