繁体   English   中英

在CppUTest中未定义的引用

[英]undefined reference to in CppUTest

我有一个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**)'

有什么想法可以尝试吗?

我将您的代码复制到我的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);
}

要运行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);
}

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

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

runtests: main.o tests.o
    g++ -o runtests  main.o tests.o  $(LD_LIBRARIES)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM