【问题标题】:conan create cannot find dependenciesconan create 找不到依赖项
【发布时间】:2020-03-16 00:35:50
【问题描述】:

我有一个项目使用conanfile.py 来管理其依赖项并为其自己的包提供配方:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Conan file for KVTest, a library used to support the development of tests.

To install dependencies view https://docs.conan.io/en/latest/howtos/vs2017_cmake.html
"""

from conans import ConanFile, CMake


class KVTestConan(ConanFile):
    name = "kvtest"
    version = "0.1.0.0"
    description = "Kiwi Test Support library"
    topics = ("kv", "kvtest", "C++")
    settings = "os", "arch", "compiler", "build_type"
    options = {"shared": [True, False]}
    default_options = {"shared": False}
    generators = "cmake_find_package"
    exports_sources = ["src/*", "include/*", "cmake/*", "CMakeLists.txt", "Config.hpp.in"]

    def requirements(self):
        self.requires("gtest/1.8.1@bincrafters/stable")
        self.requires("boost/1.70.0@conan/stable")

    def build(self):
        cmake = CMake(self)
        # Let's skip tests since the final package doesn't need them
        cmake.configure(build_folder="build/")
        cmake.build()

    def package(self):
        self.copy("*.h", dst="include", src="src")
        self.copy("*.lib", dst="lib", keep_path=False)
        self.copy("*.dll", dst="bin", keep_path=False)

    def package_info(self):
        self.cpp_info.libs = ["kvtest"]

在开发中,我导航到项目目录并执行conan install . kvtest/0.1.0.0@kiwi/testing -if="<path to Visual studio build folder>,所有依赖项都已安装,并且项目在 Visual Studio 中成功构建。

我的问题是,当我尝试为该项目创建一个包时,它找不到它的依赖项。我的CMakeLists.txt 拥有所有必需的find_package() 呼叫:

list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
find_package(gtest REQUIRED)
find_package(boost REQUIRED)

但是当我尝试创建包时它找不到任何依赖项:

kvtest/0.1.0.0@kiwi/testing: Calling build()
-- The CXX compiler identification is MSVC 19.16.27032.1
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2017/Enterprise/VC/Tools/MSVC/14.16.27023/bin/Hostx86/x64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- clang-format found: C:/Program Files/LLVM/bin/clang-format.exe
-- Could NOT find GTest (missing: GTEST_LIBRARY GTEST_INCLUDE_DIR GTEST_MAIN_LIBRARY)
-- Boost version: 1.70.0
-- Configuring done
CMake Error at src/CMakeLists.txt:50 (add_library):
  Target "kvtest" links to target "gtest::gtest" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?


CMake Error at src/CMakeLists.txt:50 (add_library):
  Target "kvtest" links to target "boost::boost" but the target was not
  found.  Perhaps a find_package() call is missing for an IMPORTED target, or
  an ALIAS target is missing?

我在其他软件包中使用它,所以我现在不知道是什么原因造成的。

【问题讨论】:

    标签: c++ cmake conan


    【解决方案1】:

    生成器cmake_find_package 不能替代作者提供的原始FindGTest.cmake。阅读下面的参考资料。

    您正在尝试使用作者提供的特定变量,如GTEST_MAIN_LIBRARY,在cmake_find_package生成器中不可用,它将不起作用。

    您可以为您的案例使用的最佳生成器是 cmake_find_package 用于 Boost,cmake_paths 用于 GTest。 GTest Conan 包包含原始的 FindGTest 和 FindGMock 文件,可让您找到所需的内容。当然,您需要在 cmake 文件中将模块名称从 find_package(gtest) 更改为 find_package(GTest)

    src/CMakeLists.txt:50 (add_library) 处的 CMake 错误: 目标“kvtest”链接到目标“boost::boost”,但目标不是 成立。 IMPORTED 目标可能缺少 find_package() 调用,或者 缺少 ALIAS 目标?

    好像你的CMAKE_MODULE_PATH 不行,不然找到Boost 没问题。

    如果您想了解有关 CMake 生成器的更多信息,可以阅读有关 transparent cmake integration 的博文。

    您可以在cmake_find_package reference 上阅读更多内容。

    【讨论】:

    • 能否请您查看我的回答?
    【解决方案2】:

    所以这是因为我在 CMake 上强制执行了源代码构建。

    我所有的根 CMakeLists.txt(我有几个项目)都有以下几行:

    if (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
      message(FATAL_ERROR "In-source builds not allowed. Please make a new directory (called a build directory) and run CMake from there. You may need to remove CMakeCache.txt.")
    endif (${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
    

    这意味着在构建目录中我需要 1) 创建构建文件夹,然后 2) 将所有 Find*.cmake 文件复制到其中:

    path = os.path.normpath(self.source_folder)
    build = os.path.join(path, "build/")
    os.mkdir(build)
    with os.scandir(path) as entries:
        for entry in entries:
            if entry.is_file() and fnmatch.fnmatch(entry.name, "Find*.cmake"):
                shutil.copy2(entry.path, build)
    

    在此之后构建正确完成。

    【讨论】:

    • GTest 定义了什么? GTEST_MAIN_LIBRARY 将不可用。你的解决方案只解决CMAKE_MODULE_PATH
    猜你喜欢
    • 1970-01-01
    • 2021-01-21
    • 2017-04-11
    • 2016-10-01
    • 2019-10-28
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多