c++ - 链接目标文件时链接器命令失败,退出代码为 1

标签 c++ linked-list linker

我正在尝试测试 Micheal T. Goodrich 等人在“C++ 中的数据结构和算法”中的 SinglyLinked List 示例。我添加了作者省略的一些细节以使其可运行。

代码如下:

#ifndef S_LINKED_LIST
#define S_LINKED_LIST

template <typename E>
class SLinkedList;

template <typename E>
class SNode
{
    private:
        E elem;
        SNode<E> * next;
        friend class SLinkedList<E>;
};
template <typename E>
class SLinkedList
{
    private:
        SNode<E> * head;
    public:
        SLinkedList();
        ~SLinkedList();
        bool empty() const;
        const E& front() const;
        void addFront(const E& e);
        void removeFront();
};
#endif /*SLinkedList.h*/

实现:

#include "SLinkedList.h"
#include <iostream>

template <typename E>
SLinkedList<E>::SLinkedList():head(NULL){}

template <typename E>
SLinkedList<E>::~SLinkedList()
{while(!empty()) removeFront();}

template <typename E>
bool SLinkedList<E>::empty() const
{return head == NULL;}

template <typename E>
const E& SLinkedList<E>::front() const
{return head->elem;}

template <typename E>
void SLinkedList<E>::addFront(const E& e)
{   
    SNode<E> * newNode = new SNode<E>;
    newNode->elem = e;
    newNode->next = head;
    head = newNode;
}

template <typename E>
void SLinkedList<E>::removeFront()
{
    SNode<E> * old = head;
    head = old->next;
    delete old;
}/*SLinkedList.cpp*/

测试文件:

#include <iostream>
#include "SLinkedList.h"

int main()
{
    SLinkedList<std::string> newlist;
    newlist.addFront("MSP");
    std::cout << newlist.front();
    return 0;
}/*test_slinkedlist.cpp*/

运行 g++ -c SLinkedList.cppg++ -c test_slinkedlist.cpp 之后 我得到目标文件 SLinkedList.otest_slinkedlist.o 没有错误。 但是当我运行 g++ -o result test_slinkedlist.o SLinkedList.o 时,出现链接器错误:

Undefined symbols for architecture x86_64:
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我花了一天时间调试这个链接器问题,但找不到。我怀疑这很明显。

操作系统:OS X


完整错误信息:

Undefined symbols for architecture x86_64:
  "SLinkedList<std::__1::basic_string<char,     std::__1::char_traits<char>, std::__1::allocator<char> >     >::addFront(std::__1::basic_string<char, std::__1::char_traits<char>,     std::__1::allocator<char> > const&)", referenced from:
      _main in test_slinkedlist.o
  "SLinkedList<std::__1::basic_string<char,     std::__1::char_traits<char>, std::__1::allocator<char> > >::SLinkedList()",     referenced from:
      _main in test_slinkedlist.o
  "SLinkedList<std::__1::basic_string<char,     std::__1::char_traits<char>, std::__1::allocator<char> >     >::~SLinkedList()", referenced from:
      _main in test_slinkedlist.o
  "SLinkedList<std::__1::basic_string<char,     std::__1::char_traits<char>, std::__1::allocator<char> > >::front() const",     referenced from:
      _main in test_slinkedlist.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

最佳答案

Implementation:

你没有说,但我们可以假设实现在 SLinkedList.cpp 中.

Undefined symbols for architecture x86_64:
...

... ,您省略了错误消息中最重要的部分。但是,我们可以猜测 SLinkedList<std::string>::addFront(...)是 Unresolved 符号之一。

您的问题是模板方法的主体(即实现)必须出现在它所在的每个编译单元中使用过,并且您将此正文放入单独的 .cpp 中违反了该规则文件,编译器在编译时可见 test_slinkedlist.cpp .

这就是为什么模板主体最常包含在 SLinkedList.h 中的原因(注意 .h ,而不是 .cpp )。另见 this answer .

附言从技术上讲,上面的“必须”是不正确的:您还可以使用显式实例化让编译器生成您需要的模板方法体。但这比将实现放入 .h 更高级一些。文件。

关于c++ - 链接目标文件时链接器命令失败,退出代码为 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38560700/

相关文章:

linker - 如何解决链接时 __gcov_init undefined reference 问题

c - 多个符号定义和静态库

c++ - boost 池的 map ?

C++:统计分布的 TR1 vs GSL vs Boost?

c - 如何将值插入哈希表上的链接列表成员

c - 将参数从动态数组传递到链表会导致覆盖值

c++ - 在C和C++中,链接器如何找到具有已实现功能的正确文件?

C++ - TC++PL RTTI dynamic_cast 向下转型和虚拟表混淆(及其实现)

c++ - OCaml 有引用传递的能力吗?

使用 RenderThread 和 UpdateThread 进行游戏的 Java LinkedList 迭代器和线程安全