Gue-Hwan Jeong
Gue-Hwan Jeong

Reputation: 23

How to use google test as only library

I have tired to run following codes.

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)

Result:

/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
...

this way is not proper in my opinion.

It seems that it is impossible to use gtest as library with general c file. Could you give me some instruction?

Upvotes: 2

Views: 752

Answers (1)

Antonio P&#233;rez
Antonio P&#233;rez

Reputation: 7002

You can't use the ASSERT_* macros anywhere, but only within a TEST() or TEST_F() macro. And you must run your tests using a test runner, which you are not doing in test.c.

Google Test is a C++ library, so you cannot link it in a C program, which doesn't mean you cannot test C code using C++. To do so, you must create a C++ program that links Google Test and a C library being your SUT (system under test).

To set up a first test project with Google Test I encourage you to read the primer section in the docs.

Upvotes: 2

Related Questions