[英]undefined reference to in CppUTest
我有一個makefile,如答案中所述:
我的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.