如何使用.c文件而不是.cpp文件在google test中编写测试类? [英] how to use a .c file to write a test class in google test instead of .cpp file?

查看:95
本文介绍了如何使用.c文件而不是.cpp文件在google test中编写测试类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将googletest用于我的Android NDK项目包含.c文件.我使用了.cpp类型的测试类来执行相同的操作.我想改用.c文件.尝试使用它时出现以下错误:

I have used googletest for my Android NDK project contain .c files. I have used a test class of the type .cpp to do the same. I want to use .c file instead. 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.

我该如何解决?

推荐答案

您不能使用.c文件而不是.cpp文件在googletest中编写测试类.

You cannot use a .c file to write a test class in googletest instead of a .cpp file.

.c文件应包含C语言源代码,并且C/C ++编译器将假定.c文件 将被编译为C.

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.

.cpp文件应包含C ++语言源代码,并且C/C ++编译器将假定.cpp文件 将被编译为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++.

C和C ++是相关的,但编程语言不同. C比C ++更老,更简单. 没有使用C语言的类.包含类的C ++源代码无法编译为C.

C and C++ are related but different programming languages. C is much older and simpler than C++. There are no classes in the C language. C++ source code containing classes cannot be compiled as C.

Googletest是一个用C ++而不是C编写的单元测试框架,它要求您编写 使用框架类在C ++中编写测试代码.您的测试必须在.cpp(和.h)文件中进行编码,以便 编译器会将它们编译为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. Your tests must coded in .cpp (and .h) files so that the compiler will compile them as C++.

但是,您可以使用googletest对C代码进行单元测试. C代码将在.c.h文件中,但是您 必须像往常一样在.cpp.h文件中编写单元测试的代码. C/C ++编译器知道 .c文件将被编译为C,而.cpp文件将被编译为C ++.

However, you can use googletest to unit-test C code. 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. 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++.

要进行#include "some_header.h"时,必须处理一点小麻烦. 在您的C ++单元测试代码中,some_header.h是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 ++编译器将处理some_header.h,并且可以正确地处理 它知道 some_header.h 是C语言头文件.通知C ++编译器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. To inform the C++ compiler that some_header.h is a C header, you write this:

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

如果不为C语言标头在#include周围放置extern "C" { ... },则将得到未定义符号 链接时出错.

If you don't put extern "C" { ... } around the #include for a C-language header then you will get undefined-symbol errors at linktime.

我建议您尝试一个包含以下三个文件的项目:

I suggest that you experiment with a project containing the following three files:

return_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

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

int return_one(void)
{
    return 1;
}

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();
}

获取此项目,以使用googletest进行编译,链接和运行.

Get this project to compile, link and run with googletest.

您可能会发现此问题的答案很有帮助.

You may find the answers to this question helpful.

这篇关于如何使用.c文件而不是.cpp文件在google test中编写测试类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆