gpt4 book ai didi

c++ - CMake 与多个 gtest 冲突

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:46:50 25 4
gpt4 key购买 nike

这是我在 StackOverflow 上的第一篇文章,如果我的问题有问题,请深表歉意。

我是 CMake 的新手,我在尝试将 gtest (Google Test) 导入 C++ 项目时遇到了问题,该项目在库中使用了现有的 gtest 目录。

我的根 CMakeLists.txt 文件是这样的(我的更改是在“开始更改”和“结束更改”之间进行的):

cmake_minimum_required(VERSION 2.6)

project(nifi-minifi-cpp)
set(PROJECT_NAME "nifi-minifi-cpp")
set(PROJECT_VERSION_MAJOR 0)
set(PROJECT_VERSION_MINOR 2)
set(PROJECT_VERSION_PATCH 0)

#### Establish Project Configuration ####
# Enable usage of the VERSION specifier
# https://cmake.org/cmake/help/v3.0/policy/CMP0048.html#policy:CMP0048
IF(POLICY CMP0048)
CMAKE_POLICY(SET CMP0048 OLD)
ENDIF(POLICY CMP0048)

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
elseif(COMPILER_SUPPORTS_CXX0X)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Search for threads
find_package(Threads REQUIRED)

# Provide custom modules for the project
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

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

# Generate source assembly
set(ASSEMBLY_BASE_NAME "${CMAKE_PROJECT_NAME}-${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH}")
set(CPACK_SOURCE_GENERATOR "TGZ")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${ASSEMBLY_BASE_NAME}-source")
set(CPACK_SOURCE_IGNORE_FILES "/build/;~$;${CPACK_SOURCE_IGNORE_FILES};${CMAKE_SOURCE_DIR}/.git/;${CMAKE_SOURCE_DIR}/.idea/;${CMAKE_SOURCE_DIR}/cmake-build-debug/")

# Generate binary assembly
install(FILES conf/minifi.properties conf/config.yml
DESTINATION conf
COMPONENT bin)

install(PROGRAMS bin/minifi.sh
DESTINATION bin
COMPONENT bin)

install(FILES LICENSE README.md NOTICE
DESTINATION .
COMPONENT bin)

set(CPACK_GENERATOR "TGZ")
set(CPACK_COMPONENT_INCLUDE_TOPLEVEL_DIRECTORY 1)
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Apache NiFi MiNiFi C++ version ${VERSION}")
set(CPACK_PACKAGE_VENDOR "Apache NiFi")
set(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
set(CPACK_PACKAGE_FILE_NAME "${ASSEMBLY_BASE_NAME}")
set(CPACK_BINARY_TGZ, "ON")

set(CPACK_ARCHIVE_COMPONENT_INSTALL ON)
set(CPACK_COMPONENTS_ALL bin)

include(CPack)

#### Begin changes
# Set up testing
enable_testing()
set(GTEST_ROOT "thirdparty/googletest/googletest")
add_subdirectory(thirdparty/googletest)
#
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(tailFileTest
test/TailFileTest.cpp
)
target_link_libraries(tailFileTest ${GTEST_LIBRARY_DEBUG} ${GTEST_MAIN_LIBRARY_DEBUG})

add_test(
NAME tailFileTest
COMMAND tailFileTest
)


#add_dependencies(${PROJECT_TEST_NAME} googletest)

#set(PROJECT_TEST_NAME ${PROJECT_NAME}_test)
#file(GLOB TEST_SRC_FILES ${PROJECT_SOURCE_DIR}/test/*.cpp)
#
#add_executable(${PROJECT_TEST_NAME} ${TEST_SRC_FILES})
#add_dependencies(${PROJECT_TEST_NAME} googletest)
#### End changes

运行 cmake .. 时 CMake 的输出(更相关的错误消息 - 可能)是:

CMake Error at thirdparty/googletest/googletest/cmake/internal_utils.cmake:151 (add_library):
add_library cannot create target "gtest" because another target with the
same name already exists. The existing target is a static library created
in source directory
"/home/dev/nifi-minifi-cpp/thirdparty/yaml-cpp-yaml-cpp-0.5.3/test/gmock-1.7.0/gtest".
See documentation for policy CMP0002 for more details.

目前,我在以下位置获得了 googletest 的拷贝:

/home/g/dev/nifi-minifi-cpp/thirdparty/googletest

现有的 googletest 目录位于:

/home/g/dev/nifi-minifi-cpp/thirdparty/yaml-cpp-yaml-cpp-0.5.3/test/gmock-1.7.0

这是目录结构的快速图片:

.
├── CMakeLists.txt
├── test [<- My directory to store and run tests]
└── thirdparty
├── googletest [<- My googletest directory]
└── yaml-cpp-yaml-cpp-0.5.3
├── test
│   ├── CMakeLists.txt
│   ├── gmock-1.7.0 [<- The existing googletest directory]

278 directories, 2096 files

总体目标是有一个用于运行测试的test目录。

对于我做错了什么或我应该做什么的任何建议或线索,将不胜感激。希望上面的内容解释了我面临的挑战,如果有人认为我遗漏了文件或错误消息,我可以将其粘贴到此处。

最佳答案

这只是使用 add_subdirectorydouble including 3d-party project(在你的例子中是 gtest)。第一个包含点是

add_subdirectory(gmock-1.7.0)    

在 yaml-cpp subproject 中,第二个包含点是

add_subdirectory(thirdparty/googletest)

在您的 CMakeLists.txt 中。

这种双重包含在 CMake 中不起作用,因此您需要消除其中一个 add_subdirectory 调用。

  1. 在您的代码中,您可以将add_subdirectory 方法替换为ExternalProject_Addexecute_process 的组合。该方法将在配置阶段构建 googletest(因此,find_package(GTest) 将起作用),但不会用来自 googletest 的目标污染项目的命名空间。

  2. 在其他项目 (yaml-cpp) 中,您可以通过禁用测试来消除包含 googletest。只需禁用适当的选项

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

    之前

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

    (要从 CMakeLists.txt 中禁用选项,您需要干净地重新配置整个项目,即使用空 CMake 缓存)。

    请注意,此选项还会禁用 yaml-cpp 项目的 util/ 子目录下的一些实用程序。

  3. 与第一种方法类似,您可以将包含 yaml-cpp 子项目更改为 ExternalProject_Add + execute_process。这样它的所有目标和它的子项目的目标都不会污染你项目的命名空间。

关于c++ - CMake 与多个 gtest 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42085151/

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