如何使用 google 测试作为唯一的库

How to use google test as only library

我已经厌倦了 运行 下面的代码。

test.c:

#include <stdio.h>
#include <gtest/gtest.h>

int main(void)
{
    ASSERT_EQ(18, 1);
    return 0;
}

CMakeList.txt:

cmake_minimum_required(VERSION 2.6)

#Locate GTest
find_package(GTest REQUIRED)

add_executable(test test.c)
add_executable(runTests test.cpp)
target_link_libraries(runTests ${GTEST_LIBRARIES} pthread)
target_link_libraries(test ${GTEST_LIBRARIES} pthread)

结果:

/usr/include/gtest/gtest.h:54:18: fatal error: limits: No such file or directory

Also, even if I includes limits.h in CMakeList.txt, new errors comes to me.

file included from /usr/include/gtest/internal/gtest-port.h:188:0,
from /usr/include/gtest/internal/gtest-internal.h:40,

/usr/include/stdlib.h:140:8: error: ?size_t? does not name a type
...

我认为这种方式不合适。

一般的c文件好像不能用gtest做库。 你能给我一些指示吗?

您不能在任何地方使用 ASSERT_* 宏,只能在 TEST()TEST_F() 宏中使用。并且您必须 运行 使用测试 运行ner 进行测试,而您在 test.c.

中没有这样做

Google Test 是一个 C++ 库,所以你不能在 C 程序中 link 它,这并不意味着你不能使用 C++ 测试 C 代码。为此,您必须创建一个 links Google 测试的 C++ 程序和一个作为您的 SUT(被测系统)的 C 库。

要使用 Google 测试设置第一个测试项目,我鼓励您阅读 primer section in the docs