cmake 使用MinGW的重定位地址错误

vxf3dgd4  于 2022-11-11  发布在  其他
关注(0)|答案(1)|浏览(189)

我在Windows 7 64位上使用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,但它不工作。

qltillow

qltillow1#

当符号具有不正确的__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源代码编译到您的项目中,您需要定义相应的宏。

[...]

# elif GTEST_CREATE_SHARED_LIBRARY

# define GTEST_API_ __declspec(dllexport)

# endif

[...]

相关问题