尝试使用UnitTest ++链接测试中的代码时出现链接器错误 [英] Linker errors when trying to link code in tests using UnitTest++

查看:102
本文介绍了尝试使用UnitTest ++链接测试中的代码时出现链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用Unittest ++(1.4版)

So I'm using Unittest++ (version 1.4)

我已经尝试进行一些虚拟测试(CHECK(true)和CHECK(false),并且工作正常).但是,一旦我尝试包含一些生产代码,链接器就会因为使用任何类而烦恼或从我提供的用于测试的标头中提取的功能.

I've tried making a couple of dummy tests (CHECK(true) and CHECK(false), and those work fine. However, as soon as I try to include some production code, the linker goes nuts over using any class or function from the headers that I've included for testing.

我的问题是:

  1. 这可以由UnitTest ++中的任何原因引起吗?
  2. 如何在Code :: Blocks中设置代码或项目以使该错误消失?

详细信息:

我以前在Java和C#中使用过单元测试,但是我从未使用过基于C ++的单元测试包.我使用Code :: Blocks 12.11作为我的IDE,使用GCC 4.6.2作为编译器来完成我的工作.

I have used unit testing before with java and C#, but I've never before used a C++ based unit testing package. I'm using Code::Blocks 12.11 as my IDE and GCC 4.6.2 as the compiler to do my work.

我已经将UnitTest ++解压缩到我的工具文件夹中,并按照 http中的教程中的说明进行了编译://wiki.codeblocks.org/index.php?title = UnitTesting 并成功构建了库文件.我已将库文件的位置添加到测试项目的设置文件中的链接器搜索目录设置中.

I have unpacked UnitTest++ to my tools folder and compiled it as explained in the tutorial at http://wiki.codeblocks.org/index.php?title=UnitTesting and have built the library file succesfully. I've added the location of the library file to the linker search directory settings in my test projects' settings file.

被测试的代码可以在自己的项目中很好地编译,但是不能从测试中调用.

The code under test compiles fine in its own project, but not when called from the tests.

我已经设置了项目,以便每个类都在其自己的测试文件中进行测试. 我有一个main.cpp将所有测试绑定在一起.

I've set up my project so that every class is tested in its own test file. I have a main.cpp to bind all the tests together.


#include UnitTest++.h

int main(int, char const *[])
{
    return UnitTest::RunAllTests();
}

我要测试的一个类是CameraLeaf:

One class that I'm trying to test is CameraLeaf:


#include SceneManagement\CameraLeaf.h 
#include Camera.hpp
#include UnitTest++.h

using namespace SceneManagement;


TEST(TestCameraLeafInitialisation)
{
    Camera * cam = new Camera();
    CameraLeaf * camLeaf = new CameraLeaf(1, 800, 600, 90.0f, cam);
    CHECK(camLeaf->getType() == 1);
}

(我使用的搜索目录包括尖括号,但在SO上无法正确显示)

(I'm using search directory includes using angled brackets, but won't show properly on SO)

导致:



-------------- Build: Release in Scene Graphs Tests (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -std=c++0x -Wall -fexceptions  -O2  -march=core2   -I"C:\tools\Catch\Catch-0.7(may 2013)\include" -IC:\tools\UnitTest++\src -IC:\Projects\Scene_Graphs  -c C:\Projects\Scene_Graphs\tests\unit\CameraLeafTestSuite.cpp -o obj\Release\unit\CameraLeafTestSuite.o
mingw32-g++.exe  -o "bin\Release\Scene Graphs Tests.exe" obj\Release\unit\CameraLeafTestSuite.o obj\Release\unit\TimeTestSuite.o "obj\Release\Scene Graphs Tests\main.o"   -s  C:\tools\UnitTest++\Deliv\Release\libUnitTest++.a 
obj\Release\unit\CameraLeafTestSuite.o:CameraLeafTestSuite.cpp:(.text+0x97): undefined reference to `Camera::Camera(std::string, glm::detail::tvec3)'
obj\Release\unit\CameraLeafTestSuite.o:CameraLeafTestSuite.cpp:(.text+0xe4): undefined reference to `SceneManagement::CameraLeaf::CameraLeaf(int, int, int, float, Camera*)'
obj\Release\unit\TimeTestSuite.o:TimeTestSuite.cpp:(.text+0x25): undefined reference to `Time::getInstance()'
collect2: ld gaf exit-status 1 terug
Process terminated with status 1 (0 minutes, 3 seconds)
3 errors, 0 warnings (0 minutes, 3 seconds)

我必须承认,我对C ++的了解不是一流的,但是到目前为止,我已经能够做到.我不太知道如何解决这个问题.单元测试位于项目的子目录中,并且应该可以通过包含搜索目录或使用../来跳至生产代码所在的级别.据我所知,代码已找到,否则编译器会向我抛出文件未找到错误.因此,我得出结论,这肯定是链接器错误.但是,这不是递归包含的情况,因为相机不需要相机叶,测试也不需要unittest ++框架中的东西.所以我现在有点茫然.

I must admit that my knowledge of C++ isn't top notch, but so far I've been able to get by. I don't quite know how to solve this problem. The unit tests are in a subdirectory of the project, and should be reachable with either search directory inclusion or using ../ to skip down to the level where the production code is. As far as I can see, the code is found, otherwise the compiler would throw me a file not found error. So I've concluded this must be a linker error. However, this isn't a case of recursive inclusion, as the camera doesn't need a cameraleaf, nor do the tests need things from the unittest++ framework. So I'm a bit at a loss now.

背景故事:

这是我完成学士学位课程后必须做的全部工作,这些版本是教学课程的人推荐的,因为它与他提供的样板代码最匹配.显然,较新版本的GCC存在一些问题.我已经完成了大部分任务,但遇到了一些问题,因此我决定毕竟要创建一些测试.

It's all part of the last assignment I have to do to complete my bachelor, these versions came recommended by the guy who's teaching the course, as it works best with the boiler plate code he provided. Apparently there are some problems with newer versions of GCC. I've completed most of the assignment, but I've run into some problems, so I've decided to create some tests after all.

推荐答案

@ R.MartinhoFernandes是正确的.这是一个链接器错误.

@R.MartinhoFernandes is right. This is a linker error.

您需要告诉链接器包括您要使用的对象.

you need to tell the linker to include the object you are trying to use.

遵循此线程 ,您将找到有关该错误的另一讨论.接受的答案建议将object.o添加到g ++链接器命令. (在codelite的IDE中,这已添加到链接器的选项文本框中.)我确定您会在code :: blocks

following this thread, you will find another discussion regarding the error. The answer accepted suggest to add the object.o to g++ linker command. (In the codelite's IDE, this is added in the linker's option textbox.) I'm sure you'll find your way in code::blocks

这个简化的示例演示了您正在寻找的链接器命令行:g++ -o yourTests CameraLeaf.o

this simplified example demonstrate the linker command line you are looking for: g++ -o yourTests CameraLeaf.o

我希望这会有所帮助.

这篇关于尝试使用UnitTest ++链接测试中的代码时出现链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆