Я столкнулся с ошибкой в googlemock, и я могу скомпилировать и запустить проект, используя eclipse и мой cmake, используя только один файл Test.cpp, используя следующий код, но ошибку, используя мой ложный класс. Пожалуйста, мне нужна помощь. Большое спасибо.
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using namespace std;
using namespace testing;
class mxDriverInterface
{
public:
mxDriverInterface(){}
virtual ~mxDriverInterface(){}
virtual bool isReady(void) = 0;
};
class mxDriverImpl : public mxDriverInterface
{
public:
mxDriverImpl(){}
bool isReady(){return true;}
};
// A mock mxDriver class
class MockMxDriver : public mxDriverInterface
{
public:
MockMxDriver(){}
virtual ~MockMxDriver(){}
MOCK_METHOD0(isReady, bool());
};
TEST(checkMockMx, instance)
{
EXPECT_TRUE(4 != 5);
}
int main(int argc, char **argv)
{
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
=============================================== ============================
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from checkMockMx
[ RUN ] checkMockMx.instance
[ OK ] checkMockMx.instance (0 ms)
[----------] 1 test from checkMockMx (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[ PASSED ] 1 test.
=============================================== ===========================
Но просто создаю экземпляр моего класса MockMxDriver следующим образом:
TEST(checkMockMx, instance)
{
MockMxDriver mockMxDriver;
EXPECT_TRUE(4 != 5);
}
... проект завершается со следующей ошибкой:
11:22:27 **** Incremental Build of configuration Release for project Test ****
"..\\..\\build.bat" all
PROJECT: Test
PROJECT: Test
CMAKE_SRC: C:/_SW/myProject/tests
PROJ_SRC: C:/_SW/myProject/tests
-- Configuring done
-- Generating done
-- Build files have been written to: C:/_SW/myProject/tests/test_src/build/googletest-download
[1/6] Performing update step for 'googletest'
Current branch master is up to date.
[2/6] No configure step for 'googletest'
[3/6] No build step for 'googletest'
[4/6] No install step for 'googletest'
[5/6] No test step for 'googletest'
[6/6] Completed 'googletest'
-- Configuring done
-- Generating done
-- Build files have been written to: C:/_SW/myProject/tests/test_src/build
[1/2] Building CXX object CMakeFiles/Test.dir/test_src/boostTest.cpp.obj
[2/2] Linking CXX executable Test.exe
FAILED: Test.exe
cmd.exe /C "cd . && C:\MinGW\bin\g++ -g3 -Wall -O0 -fmessage-length=0 CMakeFiles/Test.dir/test_src/Test.cpp.obj -o Test.exe -Wl,--out-implib,libTest.dll.a -Wl,--major-image-version,0,--minor-image-version,0 googletest-build/googlemock/gtest/libgtest.a googletest-build/googlemock/libgmock.a -lpthread googletest-build/googlemock/gtest/libgtest.a -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ."
googletest-build/googlemock/gtest/libgtest.a(gtest-all.cc.obj): In function `ZN7testing8internal5Mutex4LockEv':
C:\_SW\myProject\tests\test_src\build/googletest-src/googletest/src/gtest-port.cc:275: multiple definition of `testing::internal::Mutex::Lock()'
CMakeFiles/Test.dir/test_src/Test.cpp.obj:C:\_SW\myProject\tests\test_src\build/googletest-src/googletest/include/gtest/internal/gtest-port.h:2262: first defined here
googletest-build/googlemock/gtest/libgtest.a(gtest-all.cc.obj): In function `ZN7testing8internal5Mutex6UnlockEv':
C:\_SW\myProject\tests\test_src\build/googletest-src/googletest/src/gtest-port.cc:281: multiple definition of `testing::internal::Mutex::Unlock()'
CMakeFiles/Test.dir/test_src/Test.cpp.obj:C:\_SW\myProject\tests\test_src\build/../Test.cpp:119: first defined here
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
11:22:36 Build Finished (took 9s.37ms)
Вот файлы CMAKE:
CMakeLists.txt для моего тестового проекта, который использует CMakeLists.txt.in для googleTest.
CMakeLists.txt:
cmake_minimum_required (VERSION 2.8.3)
SET(MY_PROJECT_NAME Test)
project(${MY_PROJECT_NAME})
message("PROJECT: " ${CMAKE_PROJECT_NAME})
message("PROJECT: " ${PROJECT_NAME})
message("CMAKE_SRC: " ${CMAKE_CURRENT_SOURCE_DIR})
message("PROJ_SRC: " ${PROJECT_SOURCE_DIR})
add_definitions(-DGTEST_CREATE_SHARED_LIBRARY=1)
add_definitions(-DBUIlD_SHARED_LIBS=ON)
add_definitions(-Dgmock_build_tests=ON)
# Download and unpack googletest at configure time
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
execute_process(COMMAND "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
execute_process(COMMAND "${CMAKE_COMMAND}" --build .
WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/googletest-download" )
# Prevent overriding the parent project's compiler/linker
# settings on Windows
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Add googletest directly to our build. This adds
# the following targets: gtest, gtest_main, gmock
# and gmock_main
add_subdirectory("${CMAKE_BINARY_DIR}/googletest-src"
"${CMAKE_BINARY_DIR}/googletest-build")
# The gtest/gmock targets carry header search path
# dependencies automatically when using CMake 2.8.11 or
# later. Otherwise we have to add them here ourselves.
if(CMAKE_VERSION VERSION_LESS 2.8.11)
include_directories("${gtest_SOURCE_DIR}/include"
"${gmock_SOURCE_DIR}/include")
endif()
# set boost installation and compiler paths:
SET(BOOST_ROOT C:/MinGW/boost_1_64_0)
SET(CMAKE_C_COMPILER C:/MinGW/bin/gcc)
SET(CMAKE_CXX_COMPILER C:/MinGW/bin/g++)
SET(PROJECT_TEST_EXEC ${MY_PROJECT_NAME}.exe)
SET(REGEX_EXEC_LIB ${PROJECT_SOURCE_DIR}/regex-2.7-lib/lib/libregex.dll.a)
add_definitions(-D__CYGWIN__=1)
SET(CMAKE_CXX_FLAGS "-g3 -Wall -O0 -fmessage-length=0" CACHE STRING "project cxx flags" FORCE)
SET(CMAKE_C_FLAGS "-g3 -Wall -O0 -fmessage-length=0 -std=c99" )
SET(SOURCES
test_src/Test.cpp
)
include_directories (${BOOST_ROOT}
${PROJECT_SOURCE_DIR}/test_src/mocks # test directory
)
add_executable(${MY_PROJECT_NAME} ${SOURCES})
target_link_libraries(${MY_PROJECT_NAME} gtest)
target_link_libraries(${MY_PROJECT_NAME} gmock)
# create target to start test and format result for jenkins
add_custom_target(jenkins.test COMMAND ${PROJECT_TEST_EXEC} --log_format=XML --log_sink=results.xml --log_level=all --report_level=no --result_code=no )
И мой CMakeLists.txt.in
cmake_minimum_required(VERSION 2.8.2)
project(googletest-download NONE)
include(ExternalProject)
ExternalProject_Add(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG master
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)