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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10374404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 18:06:48  来源:igfitidea点击:

Error on compiling in Eclipse: ld: symbol(s) not found for architecture x86_64

c++eclipse

提问by user1240679

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) not found for architecture x86_64

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"在 main.cpp 中执行与 相关的操作时才会发生这种情况SingleLinkedist,否则在 main 中删除上述 SingleLinkedList,项目编译罚款。我一直在寻找关于同样错误的类似问题,但似乎没有一个能帮助我解决这个问题。

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

单链表.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

主程序

 #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;
}

回答by Adam Miller

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 文件是不正确的 C++。将其全部内联或仅将其保留在标题中。请参阅最底部的以下内容

回答by Adam Miller

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.

如果您不包含整个构建日志,那么添加您的源代码并没有多大帮助,只是说。