柯南创建找不到依赖项

时间:2019-07-10 10:50:32

标签: c++ cmake conan

我有一个项目,它使用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?

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

3 个答案:

答案 0 :(得分:0)

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

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

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

  

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

似乎您的CMAKE_MODULE_PATH不好,否则可以毫无问题地找到Boost。

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

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

答案 1 :(得分:0)

所以这是由于我在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)

此后,构建正确完成。

答案 2 :(得分:0)

尝试find_package(gtest CONFIG REQUIRED)