简体   繁体   English

如何使用 .c 文件在 google test 中编写测试类而不是 .cpp 文件?

[英]how to use a .c file to write a test class in google test instead of .cpp file?

I have used googletest for my Android NDK project contain .c files.我已将 googletest 用于包含 .c 文件的 Android NDK 项目。 I have used a test class of the type .cpp to do the same.我使用了一个 .cpp 类型的测试类来做同样的事情。 I want to use .c file instead.我想改用 .c 文件。 I get the following error when I try to use it :当我尝试使用它时出现以下错误:

Running main() from gtest_main.cc
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (1 ms total)
[  PASSED  ] 0 tests.

How can i solve this?我该如何解决这个问题?

You cannot use a .c file to write a test class in googletest instead of a .cpp file.您不能使用.c文件而不是.cpp文件在 googletest 中编写测试类。

A .c file should contain C language source code and a C/C++ compiler will assume that a .c file is to be compiled as C. .c文件应包含 C 语言源代码,C/C++ 编译器将假定.c文件将被编译为 C。

A .cpp file should contain C++ language source code and a C/C++ compiler will assume that a .cpp file is to be compiled as C++. .cpp文件应包含 C++ 语言源代码,C/C++ 编译器将假定.cpp文件将被编译为 C++。

C and C++ are related but different programming languages. C 和 C++ 是相关但不同的编程语言。 C is much older and simpler than C++. C 比 C++ 更古老也更简单。 There are no classes in the C language. C 语言中没有类。 C++ source code containing classes cannot be compiled as C.包含类的 C++ 源代码不能编译为 C。

Googletest is a unit-testing framework that is written in C++, not C, and it requires you to write your test code in C++, using the framework classes. Googletest 是用 C++ 而非 C 编写的单元测试框架,它要求您使用框架类以 C++ 编写测试代码。 Your tests must coded in .cpp (and .h ) files so that the compiler will compile them as C++.您的测试必须在.cpp (和.h )文件中编码,以便编译器将它们编译为 C++。

However, you can use googletest to unit-test C code.但是,您可以使用 googletest 对 C 代码进行单元测试。 The C code will be in .c and .h files, but you have to code your unit-tests , as usual, in .cpp and .h files. C 代码将在.c.h文件中,但您必须像往常一样在.cpp.h文件中编写单元测试 The C/C++ compiler knows that the .c files are to be compiled as C and the .cpp files are to be compiled as C++. C/C++ 编译器知道.c文件将被编译为 C,而.cpp文件将被编译为 C++。

There is a small complication that you must deal with when you want to #include "some_header.h" in your C++ unit-test code, and some_header.h is one of the C-language header files:当您想在 C++ 单元测试代码中#include "some_header.h"时,您必须处理一个小问题,而some_header.h是 C 语言头文件之一:

The C++ compiler is going to process some_header.h , and it can process it correctly as long as it knows that some_header.h is a C-language header file . C++ 编译器将处理some_header.h只要知道some_header.h是 C 语言头文件,它就可以正确处理它。 To inform the C++ compiler that some_header.h is a C header, you write this:要通知 C++ 编译器some_header.h是 C 头文件,您可以这样写:

extern "C" {
#include "some_header.h"
}

If you don't put extern "C" { ... } around the #include for a C-language header then you will get undefined-symbol errors at linktime.如果您没有将extern "C" { ... }放在 C 语言标头的#include周围,​​那么您将在链接时收到未定义符号错误。

I suggest that you experiment with a project containing the following three files:我建议您尝试一个包含以下三个文件的项目:

return_one.h返回_one.h

// return_one.h
#ifndef RETURN_ONE_H
#define RETURN_ONE_H

// A C library :)

// A C function that always return 1.
extern int return_one(void);

#endif

return_one.c返回_one.c

// return_one.c
#include "return_one.h"

int return_one(void)
{
    return 1;
}

test_return_one.cpp test_return_one.cpp

// test_return_one.cpp
#include "gtest/gtest.h"
extern "C" {
#include "return_one.h"
}

TEST(t_return_one, returns_1)
{
    EXPECT_EQ(1,return_one());  
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

Get this project to compile, link and run with googletest.获取此项目以使用 googletest 编译、链接和运行。

You may find the answers to this question helpful.您可能会发现此问题的答案很有帮助。

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

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