2

我有一个makefile,如答案中所述:

CPPUTestMakeFile 帮助链接

我在我的 cpp 文件中有:

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

然后我得到错误:

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

有什么想法可以尝试吗?

4

3 回答 3

2

确保文件的链接顺序正确。因此,如果您要从 main.o 和 tests.o 生成可执行文件“runtests”,则 LD_LIBRARIES(请参阅 CppUTest 文档)应该是最后一个。这可确保链接器知道链接 main 和测试所需的符号。

runtests: main.o tests.o
    g++ -o runtests  main.o tests.o  $(LD_LIBRARIES)
于 2019-05-14T11:18:01.457 回答
0

我将您的代码复制到我的 AllTest.ccp 主文件之一中,它运行良好。

您可能有一个旧版本的 CppUTest,它只定义了 RunAllTests() 的第二种形式

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

我通常使用 RUN_ALL_TESTS 宏,并将 argc 定义为 const char *,如下所示:

#include "CppUTest/CommandLineTestRunner.h"

int main(int ac, const char** av)
{
    return RUN_ALL_TESTS(ac, av);
}
于 2014-05-15T16:56:29.700 回答
0

要运行 cpputest 案例,您需要两个文件。一个应该包含所有测试用例,另一个应该只包含main()函数。

试试这样的 -

文件: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);
}

文件:cpputestmain.cpp

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

确保这两个文件tests位于您的 cpputest 目录中的同一文件夹 ( ) 下。并将这些文件夹链接到 make 文件中。请通过此网站了解更多信息

于 2014-08-13T05:58:58.697 回答