user1876942
user1876942

Reputation: 1501

undefined reference to in 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?

Upvotes: 4

Views: 4409

Answers (4)

Cloud Cho
Cloud Cho

Reputation: 1774

I think there could be two ways to compile your code.

Command line using GCC:
  If you install the CppUTest using apt-get, you could use GCC "-l" option like this:

g++ -o <exec name> <your file name> -lCppUTest -lCppUTestEx

CMake:
  If you use CMake, "CMakeLists.txt", you may consider installing the CppUTest from the source. Here are installation reference at their GitHub page and the section of From Source Install at their webpage.

Upvotes: 0

MikeW
MikeW

Reputation: 6120

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)

Upvotes: 2

Sathish
Sathish

Reputation: 3870

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

Upvotes: 1

user3468739
user3468739

Reputation: 21

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

Upvotes: 0

Related Questions