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

使用Conan安装POCO后,CMake无法找到该库的问题求助

问题:CMake无法找到Conan安装的POCO库

我从Conan官网下载了最新版本的Conan,执行了以下操作:

  1. 创建新C++项目,包含以下文件:

main.cpp

#include "Poco/MD5Engine.h"
#include "Poco/DigestStream.h"

#include <iostream>

int main(int argc, char** argv)
{
    Poco::MD5Engine md5;
    Poco::DigestOutputStream ds(md5);
    ds << "abcdefghijklmnopqrstuvwxyz";
    ds.close();
    std::cout << Poco::DigestEngine::digestToHex(md5.digest()) << std::endl;
    return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.25)
project(TestMailPrj)

set(CMAKE_CXX_STANDARD 20)

find_package(poco REQUIRED)

add_executable(TestMailPrj main.cpp)
target_link_libraries(${PROJECT_NAME} POCO)

conanfile.txt

[requires]
cmake/3.25.3
poco/1.12.4

[generators]
CMakeDeps
CMakeToolchain
  1. 执行命令:
conan install . --output-folder=build --build=missing

所有依赖已正确安装,但CMake仍无法找到POCO库,报错如下:

By not providing "Findpoco.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "poco", but CMake did not find one.
Could not find a package configuration file provided by "poco" with any of the following names:
pocoConfig.cmake
poco-config.cmake
Add the installation prefix of "poco" to CMAKE_PREFIX_PATH or set "poco_DIR" to a directory containing one of the above files. If "poco" provides a separate development package or SDK, be sure it has been installed.

请问哪里操作有误,或者这是bug?


解决方案

问题出在三个细节上:

  1. find_package名称大小写不匹配
    Conan为POCO生成的配置文件是PocoConfig.cmake(首字母大写),而你使用的find_package(poco REQUIRED)是全小写,CMake对包名称区分大小写,因此无法匹配到对应配置文件。

修正为:

find_package(Poco REQUIRED)
  1. 链接目标名称错误
    POCO的Conan包提供的CMake标准目标是Poco::Poco(如果只需要核心模块),或者分模块的目标如Poco::Crypto、Poco::Util等,而非你写的POCO。

修正链接语句:

target_link_libraries(${PROJECT_NAME} Poco::Poco)
  1. 未指定CMake工具链文件
    使用CMakeToolchain生成器时,conan install会在build目录下生成conan_toolchain.cmake,运行CMake时必须指定该文件才能让CMake识别Conan安装的依赖路径:
cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release

修正后的完整CMakeLists.txt

cmake_minimum_required(VERSION 3.25)
project(TestMailPrj)

set(CMAKE_CXX_STANDARD 20)

find_package(Poco REQUIRED)

add_executable(TestMailPrj main.cpp)
target_link_libraries(${PROJECT_NAME} Poco::Poco)

按照上述修改后,CMake即可正确识别并链接POCO库。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 12:35:04