谷歌测试不起作用

时间:2016-02-23 12:52:18

标签: linker cmake googletest archlinux clion

我在Arch Linux上,克隆了gtest repo,cmake-ed google test并安装完毕。结果如下:

- 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

检查链接给了我:

[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::*用于googletest库的所有调用。它没有链接。 (即:所有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)

2 个答案:

答案 0 :(得分:2)

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

target_link_libraries(mylib gtest)

target_link_libraries(mylib gtest_main gtest)

如果您没有提供自己的自定义主要功能。

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

答案 1 :(得分:0)

您没有链接到libgtest。

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

相关问题