Google 测试无效

Google test not working

我在 Arch Linux 上,克隆了 gtest 存储库,cmake-ed google 测试并安装。结果如下:

- Install configuration: ""
-- Up-to-date: /usr/local/lib/libgmock.a
-- Up-to-date: /usr/local/lib/libgmock_main.a
-- Up-to-date: /usr/local/include/gmock
-- Up-to-date: /usr/local/include/gmock/gmock-matchers.h
-- Up-to-date: /usr/local/include/gmock/gmock-generated-actions.h.pump
-- Up-to-date: /usr/local/include/gmock/gmock-spec-builders.h
-- Up-to-date: /usr/local/include/gmock/gmock-cardinalities.h
-- Up-to-date: /usr/local/include/gmock/gmock-more-actions.h
-- Up-to-date: /usr/local/include/gmock/gmock-generated-actions.h
-- Up-to-date: /usr/local/include/gmock/gmock-more-matchers.h
-- Up-to-date: /usr/local/include/gmock/gmock-actions.h
-- Up-to-date: /usr/local/include/gmock/gmock-generated-matchers.h
-- Up-to-date: /usr/local/include/gmock/gmock-generated-function-mockers.h.pump
-- Up-to-date: /usr/local/include/gmock/gmock-generated-matchers.h.pump
-- Up-to-date: /usr/local/include/gmock/gmock-generated-nice-strict.h.pump
-- Up-to-date: /usr/local/include/gmock/gmock-generated-nice-strict.h
-- Up-to-date: /usr/local/include/gmock/gmock-generated-function-mockers.h
-- Up-to-date: /usr/local/include/gmock/internal
-- Up-to-date: /usr/local/include/gmock/internal/gmock-generated-internal-utils.h.pump
-- Up-to-date: /usr/local/include/gmock/internal/custom
-- Up-to-date: /usr/local/include/gmock/internal/custom/gmock-matchers.h
-- Up-to-date: /usr/local/include/gmock/internal/custom/gmock-generated-actions.h.pump
-- Up-to-date: /usr/local/include/gmock/internal/custom/gmock-generated-actions.h
-- Up-to-date: /usr/local/include/gmock/internal/custom/gmock-port.h
-- Up-to-date: /usr/local/include/gmock/internal/gmock-generated-internal-utils.h
-- Up-to-date: /usr/local/include/gmock/internal/gmock-port.h
-- Up-to-date: /usr/local/include/gmock/internal/gmock-internal-utils.h
-- Up-to-date: /usr/local/include/gmock/gmock.h
-- Up-to-date: /usr/local/lib/libgtest.a
-- Up-to-date: /usr/local/lib/libgtest_main.a
-- Up-to-date: /usr/local/include/gtest
-- Up-to-date: /usr/local/include/gtest/gtest_prod.h
-- Up-to-date: /usr/local/include/gtest/gtest-param-test.h
-- Up-to-date: /usr/local/include/gtest/gtest-param-test.h.pump
-- Up-to-date: /usr/local/include/gtest/gtest-spi.h
-- Up-to-date: /usr/local/include/gtest/gtest-test-part.h
-- Up-to-date: /usr/local/include/gtest/gtest-typed-test.h
-- Up-to-date: /usr/local/include/gtest/gtest-message.h
-- Up-to-date: /usr/local/include/gtest/gtest-death-test.h
-- Up-to-date: /usr/local/include/gtest/gtest_pred_impl.h
-- Up-to-date: /usr/local/include/gtest/internal
-- Up-to-date: /usr/local/include/gtest/internal/gtest-internal.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-param-util-generated.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-param-util-generated.h.pump
-- Up-to-date: /usr/local/include/gtest/internal/custom
-- Up-to-date: /usr/local/include/gtest/internal/custom/gtest.h
-- Up-to-date: /usr/local/include/gtest/internal/custom/gtest-printers.h
-- Up-to-date: /usr/local/include/gtest/internal/custom/gtest-port.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-type-util.h.pump
-- Up-to-date: /usr/local/include/gtest/internal/gtest-string.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-type-util.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-linked_ptr.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-port-arch.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-death-test-internal.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-tuple.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-filepath.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-tuple.h.pump
-- Up-to-date: /usr/local/include/gtest/internal/gtest-param-util.h
-- Up-to-date: /usr/local/include/gtest/internal/gtest-port.h
-- Up-to-date: /usr/local/include/gtest/gtest.h
-- Up-to-date: /usr/local/include/gtest/gtest-printers.h

检查 linkage 得到我:

[user@machine]$ sudo ldconfig -v | grep gtest
ldconfig: Path `/usr/lib64' given more than once
ldconfig: Can't stat /usr/libx32: No such file or directory
    libgtest1.so -> libgtest1.so

结果:undefined reference to testing::* 对 google 测试库的所有调用。它没有 link。 (即:在所有 ASSERT_* 等..) 这是一个示例:

CMakeFiles/mylib.dir/tests/main.cpp.o: In function `main':
/home/user/git_repos/projects/mylib/tests/main.cpp:33: multiple definition of `main'
CMakeFiles/mylib.dir/tests/src/main.cpp.o:/home/user/git_repos/projects/mylib/tests/src/main.cpp:4: first defined here
CMakeFiles/mylib.dir/tests/src/main.cpp.o: In function `A_B_Test::TestBody()':
/home/user/git_repos/projects/mylib/tests/src/main.cpp:3: undefined reference to `testing::Message::Message()'
/home/user/git_repos/projects/mylib/tests/src/main.cpp:3: undefined reference to `testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)'
/home/user/git_repos/projects/mylib/tests/src/main.cpp:3: undefined reference to `testing::internal::AssertHelper::operator=(testing::Message const&) const'
/home/user/git_repos/projects/mylib/tests/src/main.cpp:3: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
/home/user/git_repos/projects/mylib/tests/src/main.cpp:3: undefined reference to `testing::internal::AssertHelper::~AssertHelper()'
CMakeFiles/mylib.dir/tests/src/main.cpp.o: In function `main':
/home/user/git_repos/projects/mylib/tests/src/main.cpp:5: undefined reference to `testing::InitGoogleTest(int*, char**)'

等...

我的CMakeLists.txt:

cmake_minimum_required(VERSION 3.4)
project(mylib)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(SOURCE_FILES
    src/lib1.h
)

set(TEST_FILES
    tests/lib1.h
    tests/main.cpp
)

enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

add_executable(mylib ${TEST_FILES} ${SOURCE_FILES})

在经过整整 3 天的磨合后,如果能帮助我们解决这个问题,我们将不胜感激!

(顺便说一句,我正在使用 Clion)

您没有链接到 libgtest。

如何使用 cmake 和 goggle test 的一个很好的例子可以在这里找到:https://gist.github.com/oneamtu/3734295

您已经告诉 CMake 在哪里寻找 gtest 头文件,但没有告诉 link 库。由于它安装在标准路径 /usr/local/lib 中,因此在 add_executable 命令之后的 CMakeLists.txt 中包含这样一行可能就足够了:

target_link_libraries(mylib gtest)

target_link_libraries(mylib gtest_main gtest)

如果您不提供自己的自定义主函数。

注意:mylib 是您的测试可执行文件的一种误导性名称。