gpt4 book ai didi

cmake - 如何将 gtest 与 CMake 链接?

转载 作者:行者123 更新时间:2023-12-05 00:15:19 27 4
gpt4 key购买 nike

我正在尝试创建 gtest 的最小示例与 CMake ,但我不确定我必须如何链接测试。

我已阅读 README gtest的。但是,而不是创建 CMakeLists.txt.in ,我想学习如何手动添加 gtest到我使用 CMake 的项目.

问题

  • 我无法通过 make tests 编译我的测试文件
  • 看来我的测试无法链接gtest

  • 错误信息
    Scanning dependencies of target tests
    [ 25%] Building CXX object CMakeFiles/tests.dir/tests/main_test.cc.o
    /Users/kkweon/temp/gtest_tutorial/tests/main_test.cc:5:1: warning: treating Unicode character as whitespace [-Wunicode-whitespace]
      ::testing::InitGoogleTest(&argc, argv);
    ^
    /Users/kkweon/temp/gtest_tutorial/tests/main_test.cc:5:3: warning: treating Unicode character as whitespace [-Wunicode-whitespace]
      ::testing::InitGoogleTest(&argc, argv);
    ^
    /Users/kkweon/temp/gtest_tutorial/tests/main_test.cc:6:1: warning: treating Unicode character as whitespace [-Wunicode-whitespace]
      return RUN_ALL_TESTS();
    ^
    /Users/kkweon/temp/gtest_tutorial/tests/main_test.cc:6:3: warning: treating Unicode character as whitespace [-Wunicode-whitespace]
      return RUN_ALL_TESTS();
    ^
    4 warnings generated.
    [ 50%] Building CXX object CMakeFiles/tests.dir/tests/add_test.cc.o
    [ 75%] Building CXX object CMakeFiles/tests.dir/src/add.cc.o
    [100%] Linking CXX executable tests
    Undefined symbols for architecture x86_64:
    "testing::InitGoogleTest(int*, char**)", referenced from:
    _main in main_test.cc.o
    "testing::Test::SetUp()", referenced from:
    vtable for AddTest_AddIntegers_Test in add_test.cc.o
    "testing::Test::TearDown()", referenced from:
    vtable for AddTest_AddIntegers_Test in add_test.cc.o
    "testing::Test::Test()", referenced from:
    AddTest_AddIntegers_Test::AddTest_AddIntegers_Test() in add_test.cc.o
    "testing::Test::~Test()", referenced from:
    AddTest_AddIntegers_Test::~AddTest_AddIntegers_Test() in add_test.cc.o
    "testing::Message::Message()", referenced from:
    AddTest_AddIntegers_Test::TestBody() in add_test.cc.o
    "testing::UnitTest::GetInstance()", referenced from:
    RUN_ALL_TESTS() in main_test.cc.o
    "testing::UnitTest::Run()", referenced from:
    RUN_ALL_TESTS() in main_test.cc.o
    "testing::internal::AssertHelper::AssertHelper(testing::TestPartResult::Type, char const*, int, char const*)", referenced from:
    AddTest_AddIntegers_Test::TestBody() in add_test.cc.o
    "testing::internal::AssertHelper::~AssertHelper()", referenced from:
    AddTest_AddIntegers_Test::TestBody() in add_test.cc.o
    "testing::internal::GetTestTypeId()", referenced from:
    ___cxx_global_var_init in add_test.cc.o
    "testing::internal::MakeAndRegisterTestInfo(char const*, char const*, char const*, char const*, testing::internal::CodeLocation, void const*, void (*)(), void (*)(), testing::internal::TestFactoryBase*)", referenced from:
    ___cxx_global_var_init in add_test.cc.o
    "testing::internal::GetBoolAssertionFailureMessage(testing::AssertionResult const&, char const*, char const*, char const*)", referenced from:
    AddTest_AddIntegers_Test::TestBody() in add_test.cc.o
    "testing::internal::IsTrue(bool)", referenced from:
    testing::internal::scoped_ptr<std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::reset(std::__1::basic_stringstream<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in add_test.cc.o
    testing::internal::scoped_ptr<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >::reset(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*) in add_test.cc.o
    "testing::internal::AssertHelper::operator=(testing::Message const&) const", referenced from:
    AddTest_AddIntegers_Test::TestBody() in add_test.cc.o
    "typeinfo for testing::Test", referenced from:
    typeinfo for AddTest_AddIntegers_Test in add_test.cc.o
    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    make[3]: *** [tests] Error 1
    make[2]: *** [CMakeFiles/tests.dir/all] Error 2
    make[1]: *** [CMakeFiles/tests.dir/rule] Error 2
    make: *** [tests] Error 2

    信息

    我已经下载了 googletest repository/Users/kkweon/github/googletest
    和“gtest.h”位于 /Users/kkweon/github/googletest/googletest/include/gtest/gtest.h
    我的项目结构:
    .
    ├── CMakeLists.txt
    ├── src
    │   ├── add.cc
    │   ├── add.h
    │   └── main.cc
    └── tests
    ├── add_test.cc
    └── main_test.cc

    和 CMakeLists.txt 文件
    cmake_minimum_required(VERSION 3.0)

    set(EXTRA_BINCFLAGS "-Wall -Wextra")
    set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} ${EXTRA_BINCFLAGS})

    include_directories(/Users/kkweon/github/googletest/googletest/include)
    include_directories(src)

    set(SOURCES src/add.cc)
    set(TEST_SOURCES tests/main_test.cc tests/add_test.cc)

    add_executable(main src/main.cc ${SOURCES})
    add_executable(tests ${TEST_SOURCES} ${SOURCES})

    完整源码:github

    先感谢您!

    最佳答案

    仅仅include_directories是不够的与谷歌测试。您还必须添加要编译的源。

    这是 what we do :

    我们创建了一个单独的 CMake 目标 google-test将其编译为库。我们包括 googletest文件夹到我们的源代码树,以便我们的项目和 Google 测试保持在一起。

    在您的主 CMakeLists你做add_subdirectory(googletest)googletest文件夹有自己的CMakeLists.txt文件和 include/src/文件夹(无需存储完整的 googletest 存储库!)。

    tree googletest/

    googletest/
    ├── CMakeLists.txt
    ├── include
    │   └── gtest
    │   ├── gtest-death-test.h
    ...
    └── src
    ├── gtest-all.cc
    ├── gtest.cc
    └── gtest_main.cc
    ...

    在您的 googletest/CMakeLists.txt你做:
    set(google_test_include_dirs
    "${CMAKE_CURRENT_SOURCE_DIR}"
    "${CMAKE_CURRENT_SOURCE_DIR}/include")
    include_directories(${google_test_include_dirs})
    file(GLOB_RECURSE google_test_headers ${CMAKE_CURRENT_SOURCE_DIR}/*.h)
    set(google_test_sources src/gtest-all.cc src/gtest_main.cc)
    add_library(google-test STATIC ${google_test_sources} ${google_test_headers})

    然后对于带有测试的目标,您可以执行以下操作:
    add_executable(MyTests ${my_unittests_sources})
    include_directories("${CMAKE_CURRENT_SOURCE_DIR}")
    include_directories("${CMAKE_SOURCE_DIR}/googletest/include")

    target_link_libraries(MyTests google-test)

    CMakeLists.txt , googletest/CMakeLists.txtunittests/CMakeLists.txt在我上面链接的项目中。

    关于cmake - 如何将 gtest 与 CMake 链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44733206/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com