CMake conflict with multiple gtest

前端 未结 1 2107
孤街浪徒
孤街浪徒 2021-02-15 00:12

This is my first post on StackOverflow, so apologies if there\'s something wrong with my question.

I\'m new at CMake, and I\'m running into a problem trying to import gt

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-15 01:10

    This is just double including 3d-party project (gtest in your case) using add_subdirectory. The first include point is

    add_subdirectory(gmock-1.7.0)    
    

    inside yaml-cpp subproject and the second include point is

    add_subdirectory(thirdparty/googletest)
    

    in your CMakeLists.txt.

    Such double including doesn't work in CMake, so you need to eliminate one of add_subdirectory call.

    1. In your code, you may replace add_subdirectory approach by combination of ExternalProject_Add and execute_process. That approach will build googletest on configuration stage (so, find_package(GTest) will work), but doesn't pollute namespace of your project with targets from googletest.

    2. In other project (yaml-cpp), you may eliminate including googletest by disable testing. Just disable appropriate option

      option(YAML_CPP_BUILD_TOOLS "Enable testing and parse tools" OFF)
      

      before

      add_subdirectory(thirdparty/yaml-cpp-yaml-cpp-0.5.3)
      

      (For disabling option from CMakeLists.txt you need to reconfigure the whole project cleanly, that is with empty CMake cache).

      Note, that this option also disables some utilities under util/ subdirectory of yaml-cpp project.

    3. Similarly to the first approach, you may change inclusion of yaml-cpp subproject to ExternalProject_Add + execute_process. Such a way all its targets and targets of its subprojects will not pollute namespace of your project.

    0 讨论(0)
提交回复
热议问题