问题
I use MinGW on Windows 7 64bit.
I used Google Test with NetBeans (followed Bo Qian instruction: http://www.youtube.com/watch?v=TS2CTf11k1U&feature=c4-overview-vl&list=PL5jc9xFGsL8GyES7nh-1yqljjdTvIFSsh&hd=1) and it worked correctly. Recently I tried to link Google Mock (with Google Test inside) to my project. I used Cmake and this is my CmakeLists.txt file:
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 generated Eclipse project file, which can be used with MinGW. I added few tests and code into "source" folder then I tried to build this under Eclipse, but I got many errors:
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
Classes in source files are correct, because I tested them earlier.
I tried to add some flags to compilation and reinstall MinGW, but it doesn't work.
回答1:
This usually happens when symbols have the incorrect __declspec
(i.e. when bundling the library sources in the project or linking against a static library you need to ensure that symbols are decorated as __declspec(dllexport)
.
According to the gmock README:
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.
So, depending on whether you are liking against the gmock DLL or compiling the gmock sources into your project, you need to define the corresponding macro. It looks like you are not linking against the DLL, so you probably need -DGTEST_CREATE_SHARED_LIBRARY=1
, since in gtest-port.h
you will find
[...]
# elif GTEST_CREATE_SHARED_LIBRARY
# define GTEST_API_ __declspec(dllexport)
# endif
[...]
来源:https://stackoverflow.com/questions/17773269/bad-reloc-address-using-mingw