使用MinGW的Bad Reloc地址

时间:2013-07-21 14:16:50

标签: cmake mingw googletest gmock

我在Windows 7 64bit上使用MinGW。

我使用了NetBeans的Google Test(遵循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一起使用。我在“source”文件夹中添加了一些测试和代码,然后我尝试在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,但它不起作用。

1 个答案:

答案 0 :(得分: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
[...]