You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Conan recipe生成指向包内resources文件夹的CMake变量

在Conan Recipe中添加指向resources文件夹的CMake变量

完全可以实现,针对你使用的Conan 1.x(因生成了conanbuildinfo.cmake),这里提供两种直接方案:

方案1:利用Conan内置resdirs自动生成变量

在你的Conan recipe(conanfile.py)的package_info方法中添加resdirs配置,Conan会自动在conanbuildinfo.cmake中生成对应路径变量:

from conans import ConanFile

class YourPackageConan(ConanFile):
    # 包的基本配置(name、version等)

    def package_info(self):
        # 常规头文件与库路径设置
        self.cpp_info.includedirs = ["include"]
        self.cpp_info.libdirs = ["lib"]
        # 声明resources目录
        self.cpp_info.resdirs = ["resources"]

之后在CMakeLists.txt中,可通过CONAN_RES_DIRS_<你的包名大写>变量获取路径:

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

# 示例:打印resources路径
message(STATUS "Resources directory: ${CONAN_RES_DIRS_YOURPACKAGE}")

方案2:自定义变量名(更灵活)

若需要自定义变量名称,可通过user_info在recipe中设置:

from conans import ConanFile
import os

class YourPackageConan(ConanFile):
    # 包的基本配置

    def package_info(self):
        # 设置自定义资源路径变量
        self.user_info.RESOURCES_PATH = os.path.join(self.package_folder, "resources")

在CMakeLists.txt中,通过CONAN_USER_INFO_<你的包名大写>_<变量名>调用:

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()

message(STATUS "Custom resources path: ${CONAN_USER_INFO_YOURPACKAGE_RESOURCES_PATH}")

注意:替换代码中的YourPackage/YOURPACKAGE为你实际的包名称。

内容的提问来源于stack exchange,提问作者Vip3Out

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 06:25:40