Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference to in CppUTest

Tags:

cpputest

I have a makefile as described here in the answer:

CPPUTestMakeFile Help linking

I have in my cpp file:

#include "CppUTest/CommandLineTestRunner.h"

int main(int ac, const char** av)
{
/* These checks are here to make sure assertions outside test runs don't crash */
CHECK(true);
LONGS_EQUAL(1, 1);

return CommandLineTestRunner::RunAllTests(ac, av);
}

Then I get the error:

undefined reference to `CommandLineTestRunner::RunAllTests(int, char const**)'

Any ideas what to try?

like image 205
user1876942 Avatar asked Oct 20 '25 12:10

user1876942


2 Answers

Ensure the link order of your files is correct. So if you are generating executable file 'runtests' from main.o and tests.o, the LD_LIBRARIES (see CppUTest documents) should be last. This ensures that the symbols required to link main and tests are known to the linker.

runtests: main.o tests.o
    g++ -o runtests  main.o tests.o  $(LD_LIBRARIES)
like image 171
MikeW Avatar answered Oct 23 '25 07:10

MikeW


For running the cpputest cases, you need two files. One should contain all your test cases and another one should contain only the main() function.

Try something like this-

File: cpputest1.cpp

#include "CppUTest/TestHarness.h"
TEST_GROUP(FirstTestGroup)
{
};

TEST(FirstTestGroup, FirstTest)
{
   FAIL("Fail me!");
}
TEST(FirstTestGroup, SecondTest)
{
   STRCMP_EQUAL("hello", "world");
   LONGS_EQUAL(1, 2);
   CHECK(false);
}

File: cpputestmain.cpp

#include "CppUTest/CommandLineTestRunner.h"
int main(int ac, char** av)
{
   return CommandLineTestRunner::RunAllTests(ac, av);
}

Make sure that these two files are under the same folder(tests) in your cpputest directory. And link these folder in the make file. Please go through this site for more info

like image 38
Sathish Avatar answered Oct 23 '25 07:10

Sathish