我在 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,但它不起作用。