conan profile 是描述某次构建所用工具链、编译器、标准、cpu架构等配置的快照,非全局开关;默认 profile 仅在未指定 --profile 时回退使用,不同项目需按需创建独立 profile。

conan profile 是什么,为什么不能直接改 default
Conan 的 profile 不是全局开关,而是明确描述「某次构建用哪套工具链、编译器、标准、CPU 架构」的快照。你运行 conan install 时如果不指定 --profile,它才 fallback 到 default。所以别想着“改 default 就一劳永逸”——不同项目要 debug/release、clang/gcc、x86_64/arm64,必须分 profile 管理。
怎么创建和切换 profile(实操三步)
新建 profile 的本质就是写一个 INI 风格文本,放在 ~/.conan2/profiles/ 下(Conan 2.x 路径)。名字随便起,比如 gcc-debug、clang-release-arm64。
常用做法:
- 用
conan profile detect自动生成当前环境基础 profile(但只含编译器版本,不含 build type 或 C++ 标准) - 手动编辑:运行
conan profile new gcc-debug --detect,再conan profile update settings.compiler.libcxx=libstdc++11 gcc-debug - 或者直接
touch ~/.conan2/profiles/gcc-debug,填入:
[settings] os=Linux arch=x86_64 compiler=gcc compiler.version=11 compiler.libcxx=libstdc++11 compiler.cppstd=17 build_type=Debug
使用时加 --profile=gcc-debug,例如:conan install . --profile=gcc-debug
profile 中容易写错的几个关键 setting
这些字段拼错、值不合法,Conan 会静默忽略或报奇怪错误(比如找不到包),不是所有字段都有默认值:
-
compiler.cppstd必须是字符串,如17、20,不能写c++17(那是 CMake 的写法) -
compiler.libcxx在 GCC 下必须匹配实际链接的 STL 实现:libstdc++(旧 ABI)或libstdc++11(C++11 ABI),写反会导致链接失败 -
build_type区分大小写,只能是Debug/Release/RelWithDebInfo/MinSizeRel,写成debug会被当成空值 -
os和arch影响二进制兼容性判断,交叉编译时必须显式设对,比如os=Android+arch=armv8
多 profile 协同:什么时候该用 --profile:build 和 --profile:host
只有做交叉编译(比如在 x86_64 Linux 上编译 ARM64 Android 库)才需要双 profile。这时:
-
--profile:host描述目标平台(你要跑在哪):os=Android,arch=armv8 -
--profile:build描述构建平台(你在哪编):os=Linux,arch=x86_64 - 漏掉
:build会让 Conan 把 host 当 build,找不到本地工具链(比如没装android-ndk时还硬要调clang++) - 普通开发(非交叉)只用
--profile即可,--profile:build会强制覆盖自动检测,反而坏事
profile 文件本身不包含路径或环境变量,那些得靠 conf 段或 Conan 的 tools.system.package_manager 插件补全——这是另一个容易卡住的地方。
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!











