首页 > 解决方案 > 带有 cmake-conan 的柯南缺少 conanbuild.conf

问题描述

我想用柯南打包一个 CMake 项目。为此,我使用以下内容conanfile.py

import os
from conans import ConanFile, tools
from conan.tools.cmake import CMake, CMakeToolchain
from conans.tools import Version

class BaseLibrary(ConanFile):
    name = "base-library"
    version = "1.0.0"
    description = """This is a test project with a library base::io
    and base::math and an executable cli."""
    license = "MIT"
    generators = "cmake_find_package_multi", "cmake_find_package",
    default_options = {"fmt:shared": True}
    build_policy = "missing"  # if this package is build by default if missing.
    settings = "os", "compiler", "build_type", "arch"
    exports_sources = "*"

    _cmake = None

    def requirements(self):
        if Version(self.version) >= "1.0.0":
            self.requires("fmt/8.0.1")

    def _configure_cmake(self):
        if self._cmake:
            return self._cmake
        self._cmake = CMake(self)
        self._cmake.configure(source_folder=".")
        return self._cmake

    def build(self):
        cmake = self._configure_cmake()
        cmake.build()
        cmake.install()

    def package(self):
        cmake = self._configure_cmake()
        cmake.install()
        tools.rmdir(os.path.join(self.package_folder, "lib", "cmake"))
        tools.rmdir(os.path.join(self.package_folder, "lib", "pkgconfig"))
        tools.rmdir(os.path.join(self.package_folder, "share"))

    def package_info(self):
        self.cpp_info.names["cmake_find_package"] = "base"
        self.cpp_info.names["cmake_find_package_multi"] = "base"
        self.cpp_info.names["pkg_config"] = "base"

CMakeLists.txt这是我的主文件旁边。CMake 项目的构建没有问题,并且还有一个正确的install目标,可以正确安装所有内容:bin,lib,include,share. 在 CMake 中,我使用conan-cmake模块,基本上是这样的。

当我跑

conan create -s build_type=Release . demo/testing

我收到以下奇怪的错误:

...
Requirements
    base-library/1.0.0@demo/testing from local cache - Cache
    fmt/8.0.1 from 'conancenter' - Cache
Packages
    base-library/1.0.0@demo/testing:4f2b14d304ab8e4391d162a6eb44110cc27a3faa - Build
    fmt/8.0.1:d4e9c4f02b4f03edf5a640dcd22779727d782e79 - Cache

Installing (downloading, building) binaries...
fmt/8.0.1: Already installed!
base-library/1.0.0@demo/testing: WARN: Build folder is dirty, removing it: /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa
base-library/1.0.0@demo/testing: Configuring sources in /home/developer/.conan/data/base-library/1.0.0/demo/testing/source
base-library/1.0.0@demo/testing: Copying sources to build folder
base-library/1.0.0@demo/testing: Building your package in /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa
base-library/1.0.0@demo/testing: Generator cmake_find_package created Findfmt.cmake
base-library/1.0.0@demo/testing: Generator cmake_find_package_multi created fmt-config-version.cmake
base-library/1.0.0@demo/testing: Generator cmake_find_package_multi created fmt-config.cmake
base-library/1.0.0@demo/testing: Generator cmake_find_package_multi created fmtTargets.cmake
base-library/1.0.0@demo/testing: Generator cmake_find_package_multi created fmtTarget-release.cmake
base-library/1.0.0@demo/testing: Aggregating env generators
base-library/1.0.0@demo/testing: Calling build()
base-library/1.0.0@demo/testing: 
base-library/1.0.0@demo/testing: ERROR: Package '4f2b14d304ab8e4391d162a6eb44110cc27a3faa' build failed
base-library/1.0.0@demo/testing: WARN: Build folder /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa
ERROR: base-library/1.0.0@demo/testing: Error in build() method, line 74
        cmake = self._configure_cmake()
while calling '_configure_cmake', line 65
        self._cmake = CMake(self)
        ConanException: The file /home/developer/.conan/data/base-library/1.0.0/demo/testing/build/4f2b14d304ab8e4391d162a6eb44110cc27a3faa/conanbuild.conf does not exist. Please, make sure that it was not generated in another folder.

这里有什么问题,我该如何解决?我找不到与此相关的任何内容?

标签: c++cmakeconan

解决方案


我遇到了同样的问题,最后发现了问题:
CMake正在导入的是一个正在使用的CMakeToolchain,它们都是conan.tools.cmake模块的一部分。cmake_find_package[ref]的用法中描述的那个是conans模块中的那个

所以更换:

from conans import ConanFile, tools
from conan.tools.cmake import CMake, CMakeToolchain

经过

from conans import ConanFile, tools, CMake
from conan.tools.cmake import CMakeToolchain

应该解决你的问题


推荐阅读