问题
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:
- Can this be caused by anything in UnitTest++?
- 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.
回答1:
@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.
来源:https://stackoverflow.com/questions/17276901/linker-errors-when-trying-to-link-code-in-tests-using-unittest