在Eclipse中编译时出错:ld:架构x86_64没有找到的符号

时间:2022-03-22 15:20:48

I just resumed back to working with C++ after a long time. I am using Eclipse on Macbook Pro, and I tried just making a small project of Linked Lists.

很长一段时间后,我又开始使用c++了。我正在Macbook Pro上使用Eclipse,并尝试做一个链接列表的小项目。

I have been getting this error when I try compile my project:
ld: symbol(s) not found for architecture x86_64

当我尝试编译我的项目:ld: symbol(s)时,我就遇到了这个错误

This happens only if I #include "SingleLinkedList.h" in my main.cpp and do operations related to SingleLinkedist, otherwise removing the above SingleLinkedList in main, the project compiles fines. I have been looking similar questions on SO with same error, but none seem to have helped me resolve this.

只有当我#include“SingleLinkedList”时才会发生这种情况。我的主要h”。cpp和做一些与单列链接相关的操作,否则删除上面的单列链接列表,项目将编译罚款。我一直在用同样的错误来寻找类似的问题,但似乎没有人帮助我解决这个问题。

Here are the class files if needed:
As far as I remember, earlier everything in the same way used to work fine on Eclipse. I just upgraded to Lion some time back, and installed the new Xcode and a new Eclispe, and now been facing weird issues instead of concentrating on coding.

这里是需要的类文件:据我所知,前面的所有文件都使用了在Eclipse上正常工作的方式。不久前,我刚刚升级到Lion,安装了新的Xcode和新的Eclispe,现在却面临着奇怪的问题,而不是专注于编码。

SingleLinkedList.cpp

SingleLinkedList.cpp

#include "SingleLinkedList.h"

template<class T>
SingleLinkedList<T>::~SingleLinkedList()
{
    Node<T> *temp = head;
    while(head!=0)
    {
        temp = head;
        head = temp->next;
        delete temp;
    }
}

template <class T>
void SingleLinkedList<T>::addToHead(int item)
{
    head = new Node<T>(item, head);
    if (tail==0)
        tail = head;
}

template <class T>
void SingleLinkedList<T>::addToTail(int item)
{
    if(tail!=0)
    {
    tail->next = new Node<T>(item);
    tail = tail->next;
    }
    else
        head = tail = new Node<T>(item);
}

template <class T>
int SingleLinkedList<T>::deletefromHead()
{
    int e1 = head->info;
    Node<T> *temp = head;
    if(head ==tail)
    {
    head = tail = 0;
    }
    else
        head = temp->next;
    delete temp;
    return e1;
}

template <class T>
int SingleLinkedList<T>::deletefromTail()
{
    int e1 = tail->info;
    if(head == tail)
    {
        delete head;
        head = tail = 0;
    }
    else{
        Node<T> *temp = head;
        while(temp->next != tail)
            temp = temp->next;
        delete tail;
        tail = temp;
        tail->next = 0;
    }
    return e1;
}

template <class T>
void SingleLinkedList<T>::deleteNode(int item)
{
    if(head!=0) {
        if(head == tail && head->info){ //If this is the only item in the linked list
            delete head;
            head = tail = 0;
        }
        else if (item == head->info){
            Node<T> *temp = head;
            head = temp->next;
            delete temp;
        }
        else{
            Node<T> *temp = head;
            while(temp->next->info != item && temp->next !=0 ){
                temp = temp->next;
            }
            if(temp!=0){
                temp->next = temp->next->next;
                temp = temp->next;
                delete temp;
            }
        }
    }
}

template <class T>
bool SingleLinkedList<T>::isEmpty()
{
    return head == 0;
}

template <class T>
bool SingleLinkedList<T>::isInList(int item)const
{
    Node<T> * temp = head;
    while(temp!=0)
    {
        if(temp->info == item){
            break;
        }
        temp = temp->next;
    }
    return temp->info == item;
}

template<class T>
SingleLinkedList<T>::SingleLinkedList()
{
    head = tail = 0;
}

SingleLinkedList.h

SingleLinkedList.h

#include "Node.h"

#ifndef SINGLELINKEDLIST_H_
#define SINGLELINKEDLIST_H_

template <class T>
class SingleLinkedList {
public:
    SingleLinkedList();
    ~SingleLinkedList();

    bool isEmpty();
    bool isInList(int)const;

    void addToHead(int);
    void addToTail(int);
    int deletefromHead();
    int deletefromTail();
    void deleteNode(int);
private:
    Node<T> *head;
    Node<T> *tail;
};
#endif /* SINGLELINKEDLIST_H_ */

main.cpp

main.cpp

 #include <iostream>
#include "SingleLinkedList.h"
using namespace std;
int main() {
    SingleLinkedList<int> listfile;
    listfile.addToHead(2);
    listfile.addToHead(4);
    listfile.addToHead(6);
    listfile.addToHead(8);
    listfile.addToHead(10);
return 0;
}

2 个解决方案

#1


2  

Actually, the reason this isn't working is because you don't define header files for template classes, with separate implementation, because templates dont have an implementation other than what the compiler generates. So it's incorrect c++ to have the .cpp file. Put it all inline or keep it in the header only. see the following at the very bottom

实际上,这不起作用的原因是,您没有为模板类定义头文件,没有单独的实现,因为模板只有编译器生成的实现。所以使用。cpp文件是不正确的。把它全部内联或者只放在页眉中。请看下面的部分

#2


0  

You need to make sure that they are in the build path so that eclipse knows to link the .cpp file to the rest of the binary. Go to project -> properties -> c++ general -> Paths and symbols -> source location and you should be able to set it from there. When you build again, make sure that the .cpp file that contains the implementation is being built by looking at the build log.

您需要确保它们位于构建路径中,以便eclipse知道如何将.cpp文件链接到二进制文件的其余部分。进入项目->属性-> c++通用->路径和符号->源位置,你应该能够从那里设置它。当您再次构建时,请确保包含实现的.cpp文件是通过查看构建日志构建的。

Adding your source doesn't really help much if you don't include the whole build log, just saying.

如果不包含整个构建日志,只说一句,添加源代码并没有多大帮助。

#1


2  

Actually, the reason this isn't working is because you don't define header files for template classes, with separate implementation, because templates dont have an implementation other than what the compiler generates. So it's incorrect c++ to have the .cpp file. Put it all inline or keep it in the header only. see the following at the very bottom

实际上,这不起作用的原因是,您没有为模板类定义头文件,没有单独的实现,因为模板只有编译器生成的实现。所以使用。cpp文件是不正确的。把它全部内联或者只放在页眉中。请看下面的部分

#2


0  

You need to make sure that they are in the build path so that eclipse knows to link the .cpp file to the rest of the binary. Go to project -> properties -> c++ general -> Paths and symbols -> source location and you should be able to set it from there. When you build again, make sure that the .cpp file that contains the implementation is being built by looking at the build log.

您需要确保它们位于构建路径中,以便eclipse知道如何将.cpp文件链接到二进制文件的其余部分。进入项目->属性-> c++通用->路径和符号->源位置,你应该能够从那里设置它。当您再次构建时,请确保包含实现的.cpp文件是通过查看构建日志构建的。

Adding your source doesn't really help much if you don't include the whole build log, just saying.

如果不包含整个构建日志,只说一句,添加源代码并没有多大帮助。