1

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

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.

My questions are:

  1. Can this be caused by anything in UnitTest++?
  2. How do I set up my code or the project in Code::Blocks to make this error go away?

Details:


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.

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.

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();
}

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);
}

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

results in:


-------------- 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)

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.

Background story:


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.

Onno
  • 295
  • 3
  • 21
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – R. Martinho Fernandes Jun 24 '13 at 15:25
  • 1
    No it's not. For starters, that question is really too vague. Second, it's answer doesn't really provide a means to answering my question. – Onno Jun 24 '13 at 15:31
  • It is. Linker errors *always* have the same root causes. – R. Martinho Fernandes Jun 24 '13 at 15:33
  • Like I've stated in the question, I've included all the necessary libs, there is no recursive inclusion and the main is present. – Onno Jun 24 '13 at 15:35
  • The linker says you haven't. I trust it more. And just to be clear, it's not about #includes. – R. Martinho Fernandes Jun 24 '13 at 15:36
  • Well, it is complaining about exactly those classes. When I add more production code, it complains about those as well. – Onno Jun 24 '13 at 15:36
  • If you think it's about #includes *go read the answers in the question I linked to*. Your current perception of the C++ compilation model is wrong. – R. Martinho Fernandes Jun 24 '13 at 15:37
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32279/discussion-between-onno-and-r-martinho-fernandes) – Onno Jun 24 '13 at 15:37
  • I did, and it doesn't make sense for my case. – Onno Jun 24 '13 at 15:40
  • @R.MartinhoFernandes I've tried removing the includes, and the errors are very different. I fail to see how it is a file discovery problem. Since the production code compiles and links fine by itself, it must be a problem in the test project. I'm a loss where to look, since the files are being found. How is it a generic error like you say it is? – Onno Jun 24 '13 at 16:13
  • The compilation settings for your test project don't provide all the definitions it needs (I would guess they are in Camera.o, CameraLeaf.o, and Time.o). I haven't used CodeBlocks so I can't really tell you how to do that. Does your test project somehow have a reference to the project with the code? – R. Martinho Fernandes Jun 24 '13 at 16:15

1 Answers1

2

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

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

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

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

I hope this helps.

Community
  • 1
  • 1
Simon
  • 146
  • 3
  • After a long search it turned out that Code::Blocks has to know about the files themselves as well, just including them in the sources and specifying search directories is not enough to get the job done. – Onno Nov 12 '13 at 15:31
  • Thanks man, I just started with code blocks and had an issue setting up UnitTest++ just needed to add the file at /usr/local/lib/libUnitTest++.a to the linkers list of directory's now it works great. – kiwicomb123 Nov 26 '16 at 11:07