conan入门(十五):AttributeError: ‘CMake‘ object has no attribute ‘definitions‘

conan: AttributeError: ‘CMake’ object has no attribute ‘definitions’

如下是一个简单的使用conan new--template参数指定模板为cmake_exe生成的构建exe程序的conan包定义脚本(参见我的上一篇博客《conan new 命令的新特性–模板功能(–template)》).

conanfile.py

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


class Bin2cConan(ConanFile):
    name = "bin2c"
    version = "1.0.0"

    # Optional metadata
    license = "BSD-2-Clause"
    author = "gwilymk 10km"
    url = "https://gitee.com/l0km/bin2c"
    description = "A simple utility for converting a binary file to a c application which can then be included within an application."
    topics = ("bin")

    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"

    # Sources are located in the same place as this recipe, copy them to the recipe
    exports_sources = "CMakeLists.txt", "src/*"

    def layout(self):
        cmake_layout(self)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def configure(self):
        del self.settings.compiler.libcxx
        del self.settings.compiler.cppstd

原本一切都是正常的,

当我在build函数中调用CMake.definitions函数定义一个cmake变量USE_BZ2时,执行conan create .命令报错了

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.definitions["USE_BZ2"] = False
        cmake.build()

错误输出如下:

$ conan create .
Exporting package recipe
bin2c/1.0.0 exports_sources: Copied 1 '.txt' file: CMakeLists.txt
bin2c/1.0.0 exports_sources: Copied 1 '.c' file: bin2c.c
bin2c/1.0.0: The stored package has not changed
bin2c/1.0.0: Exported revision: b6fb6e6ab5a27e021f31f7048ca1ea66
Configuration:
[settings]
arch=x86_64
arch_build=x86_64
build_type=Release
compiler=Visual Studio
compiler.runtime=MD
compiler.version=14
os=Windows
os_build=Windows
[options]
[build_requires]
[env]

bin2c/1.0.0: Forced build from source
bin2c/1.0.0 (test package): Installing package
Requirements
    bin2c/1.0.0 from local cache - Cache
Packages
    bin2c/1.0.0:63da998e3642b50bee33f4449826b2d623661505 - Build

Installing (downloading, building) binaries...
bin2c/1.0.0: Copying sources to build folder
bin2c/1.0.0: Building your package in C:\Users\guyadong\.conan\data\bin2c\1.0.0\_\_\build\63da998e3642b50bee33f4449826b2d623661505
bin2c/1.0.0: Generator txt created conanbuildinfo.txt
bin2c/1.0.0: Calling generate()
bin2c/1.0.0: Aggregating env generators
bin2c/1.0.0: Calling build()
bin2c/1.0.0:
bin2c/1.0.0: WARN: Build folder is dirty, removing it: C:\Users\guyadong\.conan\data\bin2c\1.0.0\_\_\build\63da998e3642b50bee33f4449826b2d623661505
bin2c/1.0.0: WARN: Using the new toolchains and generators without specifying a build profile (e.g: -pr:b=default) is discouraged and might cause failures and unexpected behavior
bin2c/1.0.0: ERROR: Package '63da998e3642b50bee33f4449826b2d623661505' build failed
bin2c/1.0.0: WARN: Build folder C:\Users\guyadong\.conan\data\bin2c\1.0.0\_\_\build\63da998e3642b50bee33f4449826b2d623661505\build
ERROR: bin2c/1.0.0: Error in build() method, line 32
        cmake.definitions["USE_BZ2"] = False
        AttributeError: 'CMake' object has no attribute 'definitions'

AttributeError: 'CMake' object has no attribute 'definitions'意思就是说CMake这个对象中没有definitions这个成员。

这就太意外了。

按照Conan官方文档《default_options》,《How to reuse cmake install for package() method》以及其他已经发布到conan-center上的第三方库的脚本(如cjson conanfile.py)中都是这么用的啊?为啥?

因为此类非彼类.

conans.CMakeconan.tools.cmake.CMake不是同一个类啊。

conans.CMake是较早设计并已经被广泛使用的一个类,有definitions成员。

按Conan官方说明conan.tools.cmake是比较新的还在实验阶段的一个功能,conan.tools.cmake.CMake中并没definitions成员。
在这里插入图片描述

如果不指定--template参数使用 create new $pkgname/$version生成的conanfile.py是引用的是conans.CMake

如果指定--template参数使用 create new $pkgname/$version生成的conanfile.py是引用的是conan.tools.cmake.CMake

解决方案一

改回传统的引用conans.CMake,这需要较多修改conanfile.py脚本

  • 删除所有基于conan.tools.cmake包下的引用改为conans.CMake

  • 删除layout,generate函数

下图显示修改对比
在这里插入图片描述

解决方案二

使用conan.tools.cmake.CMakeToolchain类的variables成员代替conans.CMake类的definitions成员

只需要在generate函数中增加一行代码

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["USE_BZ2"] = False
        tc.generate()

参见《variables》

下图显示修改对比
在这里插入图片描述

两种方式各有好处,你可以根据自己的需要选择

conan.tools.cmake这个实验包应该会在conan 2.0变为正式的,如果你和我一样也是刚conan入门,建议使用解决方案一,以避免今后使用中遇到其他与官方参考不一致的问题。

conan系列文章

《conan入门(一):conan 及 JFrog Artifactory 安装》
《conan入门(二):conan 服务配置-密码管理及策略》
《conan入门(三):上传预编译的库(artifact)》
《conan入门(四):conan 引用第三方库示例》
《conan入门(五):conan 交叉编译引用第三方库示例》
《conan入门(六):conanfile.txt conanfile.py的区别》
《conan入门(七):将自己的项目生成conan包》
《conan入门(八):交叉编译自己的conan包项目》
《conan入门(九):NDK交叉编译自己的conan包项目塈profile的定义》
《conan入门(十):Windows下Android NDK交叉编译Boost》
《conan入门(十一):Linux下Android NDK交叉编译Boost》
《conan入门(十二):Windows NDK 编译 boost报错:CMake was unable to find a build program … MinGW Makefile》
《conan入门(十三):conan info 命令的基本用法》
《conan入门(十四):conan new 命令的新特性–模板功能(–template)》
《conan入门(十五):AttributeError: ‘CMake‘ object has no attribute ‘definitions‘》
《conan入门(十六):profile template功能实现不同平台下profile的统一》
《conan入门(十七):支持android NDK (armv7,armv8,x86,x86_64)交叉编译的统一profile jinja2模板》
《conan入门(十八):Cannot recognize the Windows subsystem, install MSYS2/cygwin or specify a build_require》
《conan入门(十九):封装第三方开源库cpp_redis示例》
《conan入门(二十):封装只包含头文件(header_only)的库示例》
《conan入门(二十一):解决MinGW编译Openssl的编译错误:crypto/dso/dso_win32.c》
《conan入门(二十二):编译 openssl要求python 3.7以上版本》
《conan入门(二十三):Windows下MinGW编译libcurl》
《conan入门(二十四):通过CONAN_DISABLE_CHECK_COMPILER禁用编译器检查》
《conan入门(二十五):imports将包安装到本地项目或其他指定位置》
《conan入门(二十六):使用make编译makefile》
《conan入门(二十七):因profile [env]字段废弃导致的boost/1.81.0 在aarch64-linux-gnu下交叉编译失败》
《conan入门(二十八):解决conan 1.60.0下 arch64-linux-gnu交叉编译openssl/3.1.2报错问题》
《conan入门(二十九):对阿里mnn进行Conan封装塈conans.CMake和conan.tools.cmake.CMake的区别》
《conan入门(三十):对腾讯ncnn进行Conan封装》
《conan入门(三十一):在命令行(shell)中从profile中读取配置参数》
《conan 入门(三十二):package_info中配置禁用CMakeDeps生成使用项目自己生成的config.cmake》
《conan 入门(三十三):requirements()指定header的可见性(transitive_headers)》
《conan 入门(三十四):conan 2.x实现对只有Makefile的项目(erpcgen)的封装示例》
《conan 入门(三十五):在conanfile.py中获取C++编译器完整路径的方法》
《conan入门(三十六):在set_version方法中从pom.xml中读取版本号实现动态版本定义》
《conan 入门(三十七):conan 2.x通过定义环境变量(environment)执行make编译只有Makefile的项目(erpcgen)》
《conan 入门(三十八):conan二进制包的兼容性及自定义package_id的方式》
《conan入门(三十九):conan 2.x 引用第三方库示例》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值