C ++ XCODE ld:未找到架构x86_64 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)

Posted

技术标签:

【中文标题】C ++ XCODE ld:未找到架构x86_64 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)【英文标题】:C++ XCODE ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 【发布时间】:2018-06-04 04:20:03 【问题描述】:

当我尝试在 Xcode 中构建我的 c++ 程序时遇到以下错误:

架构 x86_64 的未定义符号: “DoublyLinkedList::insertFront(int*)”,引用自: _main in main.o "DoublyLinkedList::print()",引用自: _main in main.o "DoublyLinkedList::DoublyLinkedList()",引用自: _main in main.o "DoublyLinkedList::~DoublyLinkedList()",引用自: main.o ld 中的 _main:未找到架构 x86_64 clang 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 查看 调用)

当我将 '#include "DoublyLinkedList.hpp"' 在我的 'main .cpp' 文件,但我只是很困惑为什么我的头文件不会链接。我也使用了一个make文件并尝试以这种方式编译但得到同样的错误。代码如下。

DoublyLinkedList.hpp

#ifndef DoublyLinkedList_hpp
#define DoublyLinkedList_hpp

#include <stdio.h>
#include <iostream>

template <class T>
class DoublyLinkedList;

// LinkNode helper class for DoublyLinkedList
template <class T>
class LinkNode
    friend class DoublyLinkedList<T>;
public:
    LinkNode()next=NULL;prev=NULL;data=0;
    ~LinkNode()if (data)delete data;data=0;
    
private:
    T *data;
    LinkNode *next;
    LinkNode *prev;
;

//Doubly Linked List class with Templates
//insertFront - pointer to T - inserts at front
//insertRear - pointer to T - inserts at rear
template <class T>
class DoublyLinkedList
public:
    DoublyLinkedList();
    ~DoublyLinkedList();
    T *getFirst();
    T *getLast();
    int getListSize();
    void insertFront(T *);
    void insertRear(T *);
    T *removeFront();
    T *removeRear();
    void print();
private:
    LinkNode<T> *first;
    LinkNode<T> *last;
    int listSize;
;
#endif //DoublyLinkedList_hpp

DoublyLinkedList.cpp

#include "DoublyLinkedList.hpp"
template <class T>
DoublyLinkedList<T>::DoublyLinkedList()
    first = last = NULL;
    listSize=0;


//Deconstructor for DoublyLinkedList
template <class T>
DoublyLinkedList<T>::~DoublyLinkedList<T>()
    LinkNode<T> *link = first;
    while (first)
        link = first;
        first = first-> next;
        if (link->data)
            delete link->data;
            link->data=0;
        
        delete link;
    


//Getters
template <class T>
T* DoublyLinkedList<T>::getFirst() 
    return this->first;


template <class T>
T* DoublyLinkedList<T>::getLast() 
    return this->last;


template <class T>
int DoublyLinkedList<T>::getListSize() 
    return this->listSize;


//Insert node at the front of the list
template <class T>
void DoublyLinkedList<T>::insertFront(T *d)
    LinkNode<T> *link = new LinkNode<T>();
    T *nd = new T();
    *nd = *d; //default memberwise copy
    link->data = nd; //attach data
    listSize++;
    //link->prev stays at 0 (no change)
    if (first == NULL)//if empty list
        first = last = link;
        return;
    
    link->next = first;//first link becomes the second
    link->prev = link;//previous is itself
    first = link;//new node becomes first


//Insert node at the end of the list
template <class T>
void DoublyLinkedList<T>::insertRear(T *d)
    LinkNode<T> *link = new LinkNode<T>();
    T *nd = new T();
    *nd = *d;  // default memberwise copy
    link->data = nd;  // attach data
    // link->prev stays at 0 no change
    listSize++;
    if(first == NULL)
        first = last = link;
        return;
    
    last->next = link;
    link->prev = last;
    last = link;



//Removes first node and returns it
template <class T>
T *DoublyLinkedList<T>::removeFront()
    T *temp=0;
    if (first == NULL) return NULL;//if empty list

    // splice out data node
    temp = first->data;
    first->data = NULL;
    listSize--;
    if (first == last)
        delete first;
        first = last = NULL;
        return temp;
    

    LinkNode<T> *link = first;
    first->next->prev = NULL;
    first=first->next;
    delete link;
    return temp;


template <class T>
T *DoublyLinkedList<T>::removeRear()
    T *temp=NULL;
    if (last == NULL) return NULL;//if empty list

    // splice out data node
    temp = last->data;
    last->data = NULL;
    listSize--;
    if (first == last)
        delete last;
        first = last = NULL;
        return temp;
    
    LinkNode<T> *todel = last;
    last->prev->next = NULL;
    last = last->prev;
    delete todel;  // delete node that was formerly last
    return temp;


template <class T>
void DoublyLinkedList<T>::print()
    LinkNode<T> *link = first;
    while(link)
        std::cout << *(link->data) << " ";
        link = link->next;
    
    std::cout << std::endl;

main.cpp

#include <iostream>
#include "DoublyLinkedList.hpp"

int main(int argc, const char * argv[]) 
    DoublyLinkedList<int> intList;
    int w = 5;
    intList.insertFront(&w);
    intList.print();
    return 0;

制作文件

#define target, its dependencies and files
doublylinkedlist: main.o doublylinkedlist.o
    g++ -o doublylinkedlist main.o doublylinkedlist.o

#define how each object file is to be built

main.o: main.cpp
    g++ -c main.cpp

doublylinkedlist.o: DoublyLinkedList.cpp DoublyLinkedList.hpp
    g++ -c DoublyLinkedList.cpp

#clean up
clean:
    rm -f doublylinkedlist *.o *~

在此先感谢您提供任何帮助。

【问题讨论】:

【参考方案1】:

问题是由于在标头 .hpp 和实现 .cpp 文件中使用模板引起的。将实现 .cpp 代码移动到标头中并删除 .cpp 文件后,它就可以工作了。这篇文章帮助我解决/理解了这个问题:c++ template and header files。

【讨论】:

以上是关于C ++ XCODE ld:未找到架构x86_64 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)的主要内容,如果未能解决你的问题,请参考以下文章

链接器错误:ld:未找到架构 x86_64 的符号

ld:入口点(_main)未定义。对于架构 x86_64:Xcode 9

C++ 库编程错误:ld:未找到架构 x86_64 的符号

ld:未找到架构 x86_64(领域)的符号

架构 x86_64 的 Xcode 未定义符号:

错误:未找到架构 x86_64 的符号,collect2:ld 返回 1 退出状态