我有一个简单的测试文件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();
}
Run Code Online (Sandbox Code Playgroud)
我将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
Run Code Online (Sandbox Code Playgroud)
它按预期运行.
但是,我无法在Qt中编译.我的Qt项目文件,在同一目录中:
SOURCES += TestMe.cpp
INCLUDEPATH += C:\gtest-1.5.0\gtest-1.5.0\include
LIBS += -L../gtest/staticlib -lgtest
Run Code Online (Sandbox Code Playgroud)
这导致与gtest函数相关的17个"未解决的外部符号"错误.
我把头发拉到这里,因为我确信它很简单.有任何想法吗?
以下是一些未定义的外部符号:
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)
Run Code Online (Sandbox Code Playgroud)
我永远不能让它作为静态库工作,但它作为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
Run Code Online (Sandbox Code Playgroud)
然后,在编译测试项目时,您必须:
(我的理解是,只有在不提供自己的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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9901 次 |
| 最近记录: |