简体   繁体   中英

CMake using GTest doesn't work properly

I was trying to setup a CMake C++ project using googletest. I'm not realy experienced with CMake and used this guide to create my own setup.

When trying to build the project, the compiler throws errors:

.../test.cpp:12:5: error: ‘EXPECT_THAT’ was not declared in this scope
     EXPECT_THAT(a, ContainerEq(b));
     ^~~~~~~~~~~
.../test.cpp:12:5: note: suggested alternative: ‘EXPECT_GT’
     EXPECT_THAT(a, ContainerEq(b));
     ^~~~~~~~~~~
     EXPECT_GT
../test.cpp:13:31: error: ‘ContainerEq’ is not a member of ‘testing’
     EXPECT_THAT(a, ::testing::ContainerEq(b)); // doesn't work either

Parts of GTest seem to work fine though. If i comment out the second test, everything works.

Folder structure:

  • CMakeLists.txt
  • test/
    • CMakeLists.txt
    • gtest.cmake
    • test.cpp

Toplevel CMakeLists.txt

cmake_minimum_required (VERSION 3.8)
project (TestProject)
enable_testing()
add_subdirectory(test)

test/CMakeLists.txt

include(gtest.cmake)
add_executable(UnitTests test.cpp)
target_link_libraries(UnitTests libgtest)
add_test(NAME AllUnitTests COMMAND UnitTests)

test/gtest.cmake

find_package(Threads REQUIRED)

# Enable ExternalProject CMake module
include(ExternalProject)

# Download and install GoogleTest
ExternalProject_Add(
    gtest
    URL https://github.com/google/googletest/archive/master.zip
    PREFIX ${CMAKE_CURRENT_BINARY_DIR}/gtest
    # Disable install step
    INSTALL_COMMAND ""
)

# Get GTest source and binary directories from CMake project
ExternalProject_Get_Property(gtest source_dir binary_dir)

# Create a libgtest target to be used as a dependency by test programs
add_library(libgtest IMPORTED STATIC GLOBAL)
add_dependencies(libgtest gtest)

# Set libgtest properties
set_target_properties(libgtest PROPERTIES
    "IMPORTED_LOCATION" "${binary_dir}/googlemock/gtest/libgtest.a"
    "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
)

# Create a libgmock target to be used as a dependency by test programs
add_library(libgmock IMPORTED STATIC GLOBAL)
add_dependencies(libgmock gtest)

# Set libgmock properties
set_target_properties(libgmock PROPERTIES
    "IMPORTED_LOCATION" "${binary_dir}/googlemock/libgmock.a"
    "IMPORTED_LINK_INTERFACE_LIBRARIES" "${CMAKE_THREAD_LIBS_INIT}"
)

# I couldn't make it work with INTERFACE_INCLUDE_DIRECTORIES
include_directories("${source_dir}/googletest/include"
                    "${source_dir}/googlemock/include")

test/test.cpp

#include <vector>
#include "gtest/gtest.h"

TEST(SampleTest, Equal){
    EXPECT_EQ(42, 42);
}

TEST(ContainerComparison, Equal){
    const std::vector<int> a(2, 1);
    const std::vector<int> b(2, 1);

    EXPECT_THAT(a, ContainerEq(b));
    EXPECT_THAT(a, ::testing::ContainerEq(b)); // doesn't work either
}

int main(int argc, char** argv){
    ::testing::InitGoogleTest(&argc, argv);
    int ret = RUN_ALL_TESTS();
    return ret;
}

Matchers are a part of GMock . You need to include it (and link against its library).

#include <gmock/gmock.h>

GMock used to be a standalone library and is now merged into GoogleTest repository . However it has separate includes and library.

Tutorial you are following is from 2014. You can find decent and up-to-date tutorials in the GoogleTest GitHub repo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM