2

我在 Windows 7 64 位上使用 MinGW。

我在 NetBeans 中使用了 Google 测试(遵循 Bo Qian 的说明:http ://www.youtube.com/watch?v=TS2CTf11k1U&feature=c4-overview-vl&list=PL5jc9xFGsL8GyES7nh-1yqljjdTvIFSsh&hd=1 )并且它工作正常。最近我尝试将 Google Mock(内部带有 Google Test)链接到我的项目。我使用了 Cmake,这是我的 CmakeLists.txt 文件:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
PROJECT(FS_Report)

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR})

SET(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -std=c++11")

SET(GOOGLE_MOCK gmock-1.6.0)

ADD_SUBDIRECTORY(${GOOGLE_MOCK})

INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/${GOOGLE_MOCK}/include)
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/${GOOGLE_MOCK}/gtest/include)

ADD_SUBDIRECTORY(source)

ENABLE_TESTING()

FILE(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/tests)

SET(TESTNAMES Aircraft Airport Exception FlightLevel
          FlightNumber PilotID Registration Remarks
          Route)

FOREACH(test ${TESTNAMES})
    ADD_EXECUTABLE(tests/${test}.test tests/${test}Test.cpp)
    TARGET_LINK_LIBRARIES(tests/${test}.test gmock_main)
    ADD_TEST(${test} tests/${test}.test)
ENDFOREACH(test)

CMake 生成的 Eclipse 项目文件,可以与 MinGW 一起使用。我在“源”文件夹中添加了一些测试和代码,然后我尝试在 Eclipse 下构建它,但是我遇到了很多错误:

Description Resource    Path    Location    Type
make[2]: *** [CMakeFiles/tests/Aircraft.test.dir/tests/AircraftTest.cpp.obj] Error 1                C/C++ Problem
make[1]: *** [CMakeFiles/tests/Aircraft.test.dir/all] Error 2               C/C++ Problem
CMakeFiles\tests\Aircraft.test.dir/objects.a(AircraftTest.cpp.obj): bad reloc address 0x1b in section `.text$_ZN7testing8internal6StringD1Ev[__ZN7testing8internal6StringD1Ev]' FS_Report@FS_ReportWorkspace            C/C++ Problem
make[1]: *** [gmock-1.6.0/gtest/CMakeFiles/gtest.dir/all] Error 2               C/C++ Problem
make[2]: *** [tests/Aircraft.test.exe] Error 1              C/C++ Problem
make[2]: *** [gmock-1.6.0/gtest/CMakeFiles/gtest.dir/src/gtest-all.cc.obj] Error 1              C/C++ Problem
undefined reference to `Aircraft::Aircraft(std::string)'    FS_Report@FS_ReportWorkspace        line 0  C/C++ Problem
undefined reference to `Aircraft::setAircraft(std::string)' FS_Report@FS_ReportWorkspace        line 0  C/C++ Problem
recipe for target 'CMakeFiles/tests/Aircraft.test.dir/all' failed   Makefile2   /FS_Report@FS_ReportWorkspace/CMakeFiles    line 63 C/C++ Problem
undefined reference to `Aircraft::getAircraft()'    FS_Report@FS_ReportWorkspace        line 0  C/C++ Problem
make: *** [all] Error 2             C/C++ Problem
recipe for target 'all' failed  Makefile    /FS_Report@FS_ReportWorkspace   line 84 C/C++ Problem
recipe for target 'tests/Aircraft.test.exe' failed  build.make  /FS_Report@FS_ReportWorkspace/CMakeFiles/tests/Aircraft.test.dir    line 92 C/C++ Problem

源文件中的类是正确的,因为我之前测试过它们。

我尝试在编译中添加一些标志并重新安装 MinGW,但它不起作用。

4

1 回答 1

1

这通常发生在符号不正确__declspec时(即在项目中捆绑库源或链接到静态库时,您需要确保符号被修饰为__declspec(dllexport).

根据 gmock 自述文件:

To compile *gtest* as a shared library, add

  -DGTEST_CREATE_SHARED_LIBRARY=1

to the compiler flags. [...]

To compile your *tests* that use the gtest shared library, add

  -DGTEST_LINKED_AS_SHARED_LIBRARY=1

to the compiler flags.

因此,根据您是喜欢 gmock DLL 还是将 gmock 源代码编译到您的项目中,您需要定义相应的宏。看起来你没有链接到 DLL,所以你可能需要-DGTEST_CREATE_SHARED_LIBRARY=1,因为gtest-port.h你会发现

[...]
# elif GTEST_CREATE_SHARED_LIBRARY
#  define GTEST_API_ __declspec(dllexport)
# endif
[...]
于 2014-02-09T12:39:33.363 回答