mardi 26 septembre 2023

undefined reference linker error with class and main [duplicate]

I have a class and a main file as shown below:

CLinkedList.cpp

#include "CLinkedList.h"


template<typename T>
bool CLinkedList<T>::empty() const {
    return cursor == nullptr;
}
template<typename T>
const T& CLinkedList<T>::front() const {
    return cursor->next->elem;
}
template<typename T>
const T& CLinkedList<T>::back() const {
    return cursor->elem;
}
template<typename T>
void CLinkedList<T>::advance() {
    cursor = cursor->next;
}
template<typename T>
void CLinkedList<T>::add(const T &e) {
    Node<T>* u  = new Node<T>;
    u-> elem = e;
    if(empty()){
        u->next = u;
        cursor = u;
    } else {
        u->next = cursor->next;
        cursor->next = u;
    }
}
template<typename T>
void CLinkedList<T>::remove() {
    Node<T>* temp = cursor->next;
    if(temp == cursor){
        cursor = nullptr;
    }else{
        cursor->next = temp->next;
    }
    delete temp;
}
template<typename T>
CLinkedList<T>::~CLinkedList(){
    while(!empty()){
        remove();
    }
}

main.cpp

#include "CLinkedList.h"
#include <iostream>
using namespace std;
int main(){
    CLinkedList<string> playList;
    playList.add("Stayin Alive");
    cout << playList.front()<<endl;
}

I defined constructor at the class declaration and I got following when I typed

gcc CLinkedList.cpp main.cpp -o out
/usr/bin/ld: module.o: warning: relocation against `_ZSt4cout' in read-only section `.text'
/usr/bin/ld: module.o: in function `main':
testCLinkedList.cpp:(.text+0x30): undefined reference to `std::allocator<char>::allocator()'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x5d): undefined reference to `CLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x69): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x75): undefined reference to `std::allocator<char>::~allocator()'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x81): undefined reference to `CLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::front() const'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x8b): undefined reference to `std::cout'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x93): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <char, std::char_traits<char>, std::allocator<char> >(std::basic_ostream<char, std::char_traits<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x9a): 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> >&)'
/usr/bin/ld: testCLinkedList.cpp:(.text+0xa5): undefined reference to `std::ostream::operator<<(std::ostream& (*)(std::ostream&))'
/usr/bin/ld: testCLinkedList.cpp:(.text+0xb1): undefined reference to `CLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~CLinkedList()'
/usr/bin/ld: testCLinkedList.cpp:(.text+0xda): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
/usr/bin/ld: testCLinkedList.cpp:(.text+0xef): undefined reference to `std::allocator<char>::~allocator()'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x104): undefined reference to `CLinkedList<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::~CLinkedList()'
/usr/bin/ld: module.o: in function `__static_initialization_and_destruction_0(int, int)':
testCLinkedList.cpp:(.text+0x14a): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: testCLinkedList.cpp:(.text+0x165): undefined reference to `std::ios_base::Init::~Init()'
/usr/bin/ld: module.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider::~_Alloc_hider()':
testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderD2Ev[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_Alloc_hiderD5Ev]+0x18): undefined reference to `std::allocator<char>::~allocator()'
/usr/bin/ld: module.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string<std::allocator<char> >(char const*, std::allocator<char> const&)':
testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC2IS3_EEPKcRKS3_[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC5IS3_EEPKcRKS3_]+0x25): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_local_data()'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC2IS3_EEPKcRKS3_[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC5IS3_EEPKcRKS3_]+0x3a): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Alloc_hider::_Alloc_hider(char*, std::allocator<char> const&)'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC2IS3_EEPKcRKS3_[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEC5IS3_EEPKcRKS3_]+0x50): undefined reference to `std::__throw_logic_error(char const*)'
/usr/bin/ld: module.o: in function `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char const*>(char const*, char const*, std::forward_iterator_tag)::_Guard::~_Guard()':
testCLinkedList.cpp:(.text._ZZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tagEN6_GuardD2Ev[_ZZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tagEN6_GuardD5Ev]+0x27): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_dispose()'
/usr/bin/ld: module.o: in function `void std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_construct<char const*>(char const*, char const*, std::forward_iterator_tag)':
testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag]+0x5c): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_create(unsigned long&, unsigned long)'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag]+0x6e): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_data(char*)'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag]+0x81): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_capacity(unsigned long)'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag]+0x97): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_local_data()'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag]+0xb7): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_data() const'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag]+0xcd): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_S_copy_chars(char*, char const*, char const*)'
/usr/bin/ld: testCLinkedList.cpp:(.text._ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag[_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE12_M_constructIPKcEEvT_S8_St20forward_iterator_tag]+0xe8): undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_set_length(unsigned long)'
/usr/bin/ld: module.o:(.data.rel.local.DW.ref.__gxx_personality_v0[DW.ref.__gxx_personality_v0]+0x0): undefined reference to `__gxx_personality_v0'
/usr/bin/ld: warning: creating DT_TEXTREL in a PIE
collect2: error: ld returned 1 exit status

How can I solve that problem ?

Aucun commentaire:

Enregistrer un commentaire