在Windows中从Qt使用Google Test [英] Use Google Test from Qt in Windows

查看:56
本文介绍了在Windows中从Qt使用Google Test的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的测试文件TestMe.cpp:

I have a simple test file, TestMe.cpp:

#include <gtest/gtest.h>

TEST(MyTest, SomeTest) {
  EXPECT_EQ(1, 1);
}

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

我将Google Test构建为静态库.(我可以提供相关的makefile.)

I have Google Test built as a static library. (I can provide the makefile if it's relevant.)

我可以从命令行毫无问题地编译TestMe.cpp:

I can compile TestMe.cpp from a command-line with no problem:

g++ TestMe.cpp -IC:\gtest-1.5.0\gtest-1.5.0\include -L../gtest/staticlib -lgtest -o TestMe.exe

它按预期运行.

但是,我无法在Qt中进行编译.我的Qt项目文件,在同一目录中:

However, I cannot get this to compile in Qt. My Qt project file, in the same directory:

SOURCES += TestMe.cpp
INCLUDEPATH += C:\gtest-1.5.0\gtest-1.5.0\include
LIBS += -L../gtest/staticlib -lgtest

这会导致与gtest函数相关的17个无法解析的外部符号"错误.

This results in 17 "unresolved external symbol" errors related to gtest functions.

我在这里拔头发,因为我敢肯定这很简单.有什么想法吗?

I'm pulling my hair out here, as I'm sure it's something simple. Any ideas?

以下是一些未定义的外部符号:

Here are some of the external symbols that are undefined:

TestMe.obj:-1: error:  unresolved external symbol "public: int __thiscall testing::UnitTest::Run(void)" (?Run@UnitTest@testing@@QAEHXZ) referenced in function _main
TestMe.obj:-1: error:  unresolved external symbol "public: static class testing::UnitTest * __cdecl testing::UnitTest::GetInstance(void)" (?GetInstance@UnitTest@testing@@SAPAV12@XZ) referenced in function _main
TestMe.obj:-1: error:  unresolved external symbol "void __cdecl testing::InitGoogleTest(int *,char * *)" (?InitGoogleTest@testing@@YAXPAHPAPAD@Z) referenced in function _main
TestMe.obj:-1: error:  unresolved external symbol "public: __thiscall testing::internal::AssertHelper::~AssertHelper(void)" (??1AssertHelper@internal@testing@@QAE@XZ) referenced in function "private: virtual void __thiscall MyTest_SomeTest_Test::TestBody(void)" (?TestBody@MyTest_SomeTest_Test@@EAEXXZ)

推荐答案

我永远无法使它作为静态库工作,但它作为DLL工作.

I never could get this to work as a static library, but it's working as a DLL.

首先,我必须将Google Test构建为DLL.使它在Visual Studio中运行没有任何成功,所以我只使用了mingw32-make.您可以使用源代码中提供的Makefile,进行以下更改:

First, I had to build Google Test as a DLL. I did not have any success getting this to work in Visual Studio, so I just used mingw32-make. You can use the Makefile provided in the source, making the following changes:

gtest-all.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -DGTEST_CREATE_SHARED_LIBRARY=1 -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $(GTEST_SRCS_)
    $(CXX) $(CPPFLAGS) -DGTEST_CREATE_SHARED_LIBRARY=1 -I$(GTEST_DIR) $(CXXFLAGS) -c \
            $(GTEST_DIR)/src/gtest_main.cc

gtest.dll : gtest-all.o
    $(CXX) -shared -o $@ $^ -Wl,--out-implib,gtest_dll.lib

gtest_main.dll : gtest-all.o gtest_main.o
    $(CXX) -shared -o $@ $^ -Wl,--out-implib,gtest_main_dll.lib

然后,在编译测试项目时,您必须:

Then, when compiling your test project, you must:

  • 定义GTEST_LINKED_AS_SHARED_LIBRARY = 1
  • 设置对gtest_dll.lib或gtest_main_dll.lib的库引用.
  • 将gtest.dll或gtest_main.dll粘贴到可执行文件所在的目录中.

(我的理解是,仅当您不提供自己的main()函数时,才使用gtest_main.)

(My understanding is that you use gtest_main only if you are NOT providing your own main() function.)

这是一个基于Qt pro的示例文件,我已经知道它正在(最终!)正常工作:

Here is a sample Qt pro file based on the one I have this is (finally!) working:

DEFINES += GTEST_LINKED_AS_SHARED_LIBRARY=1
SOURCES += main.cpp MyClassTests.cpp
INCLUDEPATH += ../path/to/gtest/includes
LIBS += -L../path/to/gtest/libraries -lgtest_dll \
    -L../ClassLibrary/bin -lMyClass
CONFIG += console

这篇关于在Windows中从Qt使用Google Test的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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