简体   繁体   English

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)

I'm getting the following errors when I try to build my c++ program in Xcode: 尝试在Xcode中构建c ++程序时出现以下错误:

Undefined symbols for architecture x86_64: 架构x86_64的未定义符号:
"DoublyLinkedList::insertFront(int*)", referenced from: _main in main.o "DoublyLinkedList::print()", referenced from: _main in main.o "DoublyLinkedList::DoublyLinkedList()", referenced from: _main in main.o "DoublyLinkedList::~DoublyLinkedList()", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) “ DoublyLinkedList :: insertFront(int *)”,引用自:main.o中的_main“ DoublyLinkedList :: print()”,引用于:main.o中的_main main.o“ DoublyLinkedList ::〜DoublyLinkedList()”,引用自:main.o中的_main ld:找不到架构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用)

My code works when I change the ' #include "DoublyLinkedList.hpp "' to ' #include "DoublyLinkedList.cpp" ' in my ' main.cpp ' file, but I'm just confused why my header file won't link. 当我改变“ 的#include‘DoublyLinkedList.hpp’”‘#包括‘DoublyLinkedList.cpp’’我‘ 的main.cpp’文件,但我只是困惑,为什么我的头文件不能链接我的代码工作。 I've also used a make file and try to compile that way but get the same error. 我还使用了一个make文件,并尝试以这种方式进行编译,但得到相同的错误。 Code below. 下面的代码。

DoublyLinkedList.hpp 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 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 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;
}

makefile 生成文件

#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 *~

Thanks in advance for any help with this. 在此先感谢您的任何帮助。

The problem was caused by using templates in the header .hpp and implementation .cpp files. 该问题是由在标题.hpp和实现.cpp文件中使用模板引起的。 After moving the implementation .cpp code into the header and removing the .cpp file it worked. 将实现.cpp代码移到头文件中并删除.cpp文件后,它可以工作。 This post helped me fix/understand the issue: c++ template and header files . 这篇文章帮助我解决/理解了这个问题: c ++模板和头文件

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用) - ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) C ++编译错误:ld:找不到架构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看调用) - C++ compilation error: ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) 在体系结构x86_64 clang中找不到符号:错误:链接器命令失败,退出代码为1(使用-v查看调用) - symbol(s) not found in architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation) ld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码为1(使用-v查看 - ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see “ ld:找不到体系结构x86_64的符号”上的“铛:错误:链接器命令失败,退出代码为1” - “clang: error: linker command failed with exit code 1” on “ld: symbol(s) not found for architecture x86_64” clang:错误:linker 命令失败,退出代码为 1(使用 -v 查看调用) - 架构 x86_64 的未定义符号: - clang: error: linker command failed with exit code 1 (use -v to see invocation) - Undefined symbols for architecture x86_64: Swift Static Library based on C++ sources: linker command failed error: ld: symbol(s) not found for architecture x86_64 clang - Swift Static Library based on C++ sources: linker command failed error: ld: symbol(s) not found for architecture x86_64 clang C++ 错误:ld:未找到体系结构 arm64 的符号 clang:错误:链接器命令失败,退出代码为 1 - C++ Error: ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 编译我的 C++ 程序:clang: error: linker command failed with exit code 1(使用 -v 查看调用) - Compiling my c++ program: clang: error: linker command failed with exit code 1 (use -v to see invocation) ld:找不到用于-lboost_system clang的库:错误:链接器命令失败,退出代码为1(使用-v查看调用) - ld: library not found for -lboost_system clang: error: linker command failed with exit code 1 (use -v to see invocation)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM