简体   繁体   English

在CppUTest中未定义的引用

[英]undefined reference to in CppUTest

I have a makefile as described here in the answer: 我有一个makefile,如答案中所述:

CPPUTestMakeFile Help linking CPPUTestMakeFile帮助链接

I have in my cpp file: 我的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);
}

Then I get the error: 然后我得到错误:

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

Any ideas what to try? 有什么想法可以尝试吗?

I copied your code into one of my AllTest.ccp main files and it worked fine. 我将您的代码复制到我的AllTest.ccp主文件之一中,并且工作正常。

You may have an old version of CppUTest that only defines the second form of RunAllTests() 您可能有一个旧版本的CppUTest,它仅定义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: 我通常使用RUN_ALL_TESTS宏,并将argc定义为const char *,如下所示:

#include "CppUTest/CommandLineTestRunner.h"

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

For running the cpputest cases, you need two files. 要运行cpputest情况,您需要两个文件。 One should contain all your test cases and another one should contain only the main() function. 一个应包含所有测试用例,而另一个应仅包含main()函数。

Try something like this- 尝试这样的事情-

File: cpputest1.cpp 文件: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 文件: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. 确保这两个文件位于cpputest目录中的同一文件夹( tests )下。 And link these folder in the make file. 并将这些文件夹链接到make文件中。 Please go through this site for more info 请通过该网站获取更多信息

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. 因此,如果要从main.o和tests.o生成可执行文件“ runtests”,则LD_LIBRARIES(请参阅CppUTest文档)应该位于最后。 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)

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

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