问题描述
我在Windows 7 64位使用MinGW。
我使用Google Test with NetBeans(按照Bo Qian的指示:),它工作正常。最近我试图链接Google Mock(与Google测试内部)到我的项目。我使用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(来源)
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}.test)
ENDFOREACH(test)
CMake生成Eclipse项目文件,可与MinGW一起使用。我添加了几个测试和代码到源文件夹,然后我试图在Eclipse下构建这个,但我有很多错误:
描述资源路径位置类型
make [2]:*** [CMakeFiles / tests / Aircraft.test.dir / tests / AircraftTest.cpp.obj]错误1 C / C ++ Problem
make [1] :*** [CMakeFiles / tests / Aircraft.test.dir / all]错误2 C / C ++问题
CMakeFiles\tests\Aircraft.test.dir / objects.a(AircraftTest.cpp.obj):坏的重定位地址0x1b在部分`.text $ _ZN7testing8internal6StringD1Ev [__ ZN7testing8internal6StringD1Ev]'FS_Report @ FS_ReportWorkspace C / C ++ Problem
make [1]:*** [gmock-1.6.0 / gtakeFiles / gtest.dir / all ]错误2 C / C ++问题
make [2]:*** [tests / Aircraft.test.exe]错误1 C / C ++问题
make [2]:*** [gmock-1.6 .0 / gtest / CMakeFiles / gtest.dir / src / gtest-all.cc.obj]错误1 C / C ++问题
未定义引用Aircraft :: Aircraft(std :: string)FS_Report @ FS_ReportWorkspace 0 C / C ++问题
未定义引用`Aircraft :: setAircraft(std :: string)'FS_Report @ FS_ReportWorkspace line 0 C / C ++ Problem
目标配方`CMakeFiles / tests / Aircraft.test.dir / all'失败Makefile2 / FS_Report @ FS_ReportWorkspace / CMakeFiles line 63 C / C ++ Problem
未定义引用`Aircraft :: getAircraft()'FS_Report @ FS_ReportWorkspace line 0 C / C ++ Problem
make:***错误2 C / C ++问题
目标'all'失败的食谱Makefile / FS_Report @ FS_ReportWorkspace line 84 C / C ++问题
目标'tests / Aircraft.test.exe'的配方失败构建。 make /FS_Report@FS_ReportWorkspace/CMakeFiles/tests/Aircraft.test.dir line 92 C / C ++ Problem
源文件中的类是正确的,因为我之前测试过它们。
我试图添加一些标志来编译和重新安装MinGW,但是它不工作。
__ declspec
(即,当捆绑项目中的库源或链接到静态库,你需要请确保符号装饰为 __ declspec(dllexport)
。 根据gmck README:
要将* gtest *编译为共享库,请添加
-DGTEST_CREATE_SHARED_LIBRARY = 1
[...]
要编译使用gtest共享库的* tests *,请添加
-DGTEST_LINKED_AS_SHARED_LIBRARY = 1
到编译器标志
所以,根据你喜欢gmock DLL还是编译你需要定义相应的宏,看起来你没有链接到DLL,所以你可能需要 -DGTEST_CREATE_SHARED_LIBRARY = 1
,因为在 gtest-port.h
您会发现
#elif GTEST_CREATE_SHARED_LIBRARY
#define GTEST_API_ __declspec(dllexport)
#endif
[...]
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.
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
[...]
这篇关于使用MinGW错误重定位地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!