2

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?

Community
  • 1
  • 1
user1876942
  • 1,411
  • 2
  • 20
  • 32
  • Did you solve this? I have the same issue on Archlinux installed from AUR – LiquidityC Sep 09 '14 at 14:04
  • I did fix it but sorry, I don't remember how. I think I did not link to the library properly. – user1876942 Sep 11 '14 at 06:33
  • I managed to solve this too. It was the ordering of the arguments to the linker that was messing my compile up: "$(LD) $(LDFLAGS) $(TEST_OBJECTS) $(LIBS) -o $@" was the working order. – LiquidityC Oct 17 '14 at 07:06

3 Answers3

2

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)
MikeW
  • 5,504
  • 1
  • 34
  • 29
  • As a relative newbie when it comes to gcc, make, and GNU build tools in general, I had no idea that the order of flags mattered that much when trying to build things. Makes sense now that I know but it didn't even cross my mind at all in the last few hours of banging my head on my desk trying to get this to work... (okay, the actual banging only lasted a few minutes, but I have been at this for hours...) so thank you very much for saving my poor skull. – RTHarston Jan 16 '20 at 17:15
  • In this case, it's not such much a "flag" as a list of library files. Order can be important, for allowing you to override library functions with your own. – MikeW Jul 22 '21 at 08:14
0

I copied your code into one of my AllTest.ccp main files and it worked fine.

You may have an old version of CppUTest that only defines the second form of RunAllTests()

static int RunAllTests(int ac, const char** av);
static int RunAllTests(int ac, char** av);

I usually use the RUN_ALL_TESTS macro, and define argc as const char *, like this:

#include "CppUTest/CommandLineTestRunner.h"

int main(int ac, const char** av)
{
    return RUN_ALL_TESTS(ac, av);
}
0

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

dwj
  • 3,443
  • 5
  • 35
  • 42
Sathish
  • 3,740
  • 1
  • 17
  • 28