在 Windows 中使用 Qt 的 Google Test

发布于 2024-08-31 13:06:57 字数 1635 浏览 6 评论 0原文

我有一个简单的测试文件 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。)

我可以从命令行编译 TestMe.cpp,没有问题:

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

它按预期运行。

但是,我无法在 Qt 中编译它。我的 Qt 项目文件,位于同一目录中:

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

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

我在这里把我的头发拉出来,因为我确信这很简单。有什么想法吗?

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

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)

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

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

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

It runs as expected.

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

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-09-07 13:06:58

我认为你的 qmake 文件没问题。但为什么 INCLUDEPATH 是绝对的而 LIBS 是相对的。我也会尝试将 LIBS 设置为绝对。

从这里
http://doc.trolltech.com/4.6/qmake-variable- Reference.html#includepath

但主要问题是什么(我认为)你需要在 INCLUDEPATH 中添加斜杠。在文档中是这样的。

INCLUDEPATH += C:/gtest-1.5.0/gtest-1.5.0/include

I think you are ok for your qmake file. But Why is INCLUDEPATH absolute and LIBS relative. I would try setting LIBS absolute also.

From here
http://doc.trolltech.com/4.6/qmake-variable-reference.html#includepath

But what the main problem is (I think) you need to put forward slashes in INCLUDEPATH. In the docs it is like this.

INCLUDEPATH += C:/gtest-1.5.0/gtest-1.5.0/include

我永远无法让它作为静态库工作,但它作为 DLL 工作。

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

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

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

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

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

这是一个基于我(终于!)工作的 Qt pro 文件示例:

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

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

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:

  • Define GTEST_LINKED_AS_SHARED_LIBRARY=1
  • Set a library reference to either gtest_dll.lib or gtest_main_dll.lib.
  • Paste gtest.dll or gtest_main.dll in the same directory as your executable.

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

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
千纸鹤带着心事 2024-09-07 13:06:57

我使用 Qt + gtest/gmock 没有任何问题。我刚刚测试了具有不同斜杠的绝对/相对路径的所有可能组合,但我无法重现您的问题。您是否检查过 qmake 生成的 Makefile.Debug 中“LIBS”变量的内容?

这里有一些通用的建议:不要使用任何绝对路径,因为你的代码不会在你自己的机器上编译,除非你将它下载到完全相同的位置(由于不同的 Qt 设置,这可能是不可能的) , ETC。)。使用相对路径,也适用于第 3 方库。

我将第三方库保留在版本控制系统中(你使用一个,对吧?)。我有一个“3rdparty”目录,对于使用这些库的每个项目,我添加 svn:external 属性,指向明确指定的 3rd party lib 版本。最后一部分很重要,因为它确保您能够构建项目的每个版本,即使您更新了第 3 方库也是如此。

I'm using Qt + gtest/gmock without any issues. I've just tested all possible combinations of absolute/relative paths with different slashes, but I couldn't reproduce your problem. Have you checked the contents of "LIBS" variable from Makefile.Debug generated by qmake?

Here's some generic piece of advice: don't use any absolute paths, because your code won't compile on other machines than your own, unless you'll download it to exacly same location (which might not be possible due to different Qt setup, etc.). Use relative paths instead, also for 3rd party libs.

I keep the 3rd party libraries in version control system (you use one, right?). I have a "3rdparty" directory and for each project that uses those libs, I add svn:external property pointing to explicitly specified version of 3rd party lib. The last part is important, because it ensures that you'll be able to build every revision of your project, even when you update 3rd party library.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文