c++ - cpp 链接器失败,无法追踪错误源

标签 c++ linker-errors

<分区>

我是 cpp 的新手,我偶然发现了一个我无法追踪的链接器故障。

我正在尝试 Google Test ,并且我尝试将内联 header 示例 ( sample-3inl.h ) 重写为单独的源文件和 header 文件。

我的文件夹结构是这样的:

Project structure:
├── src
│   ├── queue.cpp
│   ├── queue.h
│   ├── CMakeLists.txt
├── test
│   ├── lib
│   │   ├── gtest
│   │   │   ├── googletest   
│   ├── unit_test
│   │   ├── unit_test.cpp
│   │   ├── CMakeLists.txt
│   ├── CMakeLists.txt
├── main.cpp
├── CMakeLists.txt

Google Test工作正常,我已经用原来的 sample-3inl.h 试过了.

链接器失败错误:

Undefined symbols for architecture x86_64:
  "Queue<int>::Enqueue(int const&)", referenced from:
      QueueTest::SetUp() in dummy.cpp.o
  "Queue<int>::Queue()", referenced from:
      QueueTest::QueueTest() in dummy.cpp.o
  "Queue<int>::~Queue()", referenced from:
      QueueTest::QueueTest() in dummy.cpp.o
      QueueTest::~QueueTest() in dummy.cpp.o
  "Queue<int>::Size() const", referenced from:
      QueueTest_IsEmptyInitially_Test::TestBody() in dummy.cpp.o
ld: symbol(s) not found for architecture x86_64

源代码(myqueue.cpp、myqueue.h 和 unit_test.cpp)可以在以下 page 找到为简单起见。

几个小时以来一直试图找出导致错误的原因,但我找不到,真的很想知道我做错了什么。 感谢您提前抽出时间。

最佳答案

I was trying out Google Test, and I tried to rewrite the inline header sample (sample-3inl.h) to a separate source and header file.

您不能使用模板来做到这一点。有关详细信息,请参阅 Why can templates only be implemented in the header file?

关于c++ - cpp 链接器失败,无法追踪错误源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34671987/

相关文章:

c++ - 查找文本的宽度

C++:我应该将构造函数主体中分配给的指针成员初始化为 NULL 吗?

c++ - 如何刻意内联所有指定的STL代码?

c++ - 如何在 VS 中重建预编译头文件?

c - flex 的链接器错误

c++ - Qt 链接器错误 : "undefined reference to vtable"

c++ - UPC 共享数组

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

linker-errors - yaml-cpp:链接失败

windows - 'Go' Windows 端口构建问题 : 8l linker crashes