c++cmakepoco-librariesconanconan-2

CMake can not find POCO when installed with Conan


I downloaded the last version of Conan from here and then I've done the following.

  1. Creating a new c++ project with the following code

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;
 }

CMakeList.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. Run the following command: conan install . --output-folder=build --build=missing

Now, even if my all dependencies were installed properly, in CMakeFile it can not find my Poco library.

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.

Dose anyone knows what exactly I am doing wrong or this is a bug?


Solution

  • Your example is almost good to be built, but needs some updates:

    The cmake package is tool requirement, not a regular build, as you will need to run cmake only when building your project, not for runtime. Thus, you need to update your conanfile.txt to:

    [requires]
    poco/1.12.4
    
    [tool_requires]
    cmake/3.25.3
    
    [generators]
    CMakeDeps
    CMakeToolchain
    

    Second, you CMakeLists.txt is looking for poco target, but Conan will generate FindPoco.cmake instead, so you need to update not only it, but also the target:

    cmake_minimum_required(VERSION 3.25)
    project(TestMailPrj CXX)
    
    find_package(Poco REQUIRED Foundation Crypto CONFIG)
    
    add_executable(TestMailPrj main.cpp)
    target_link_libraries(${PROJECT_NAME} PRIVATE Poco::Foundation Poco::Crypto)
    target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
    

    You really don't need C++20 here, C++11 is enough for your example, so I updated it. Plus, you are using MD5Engine and DigestStream that are part of Crypto module. The Foundation module is basic and always needed. You could use Poco::Poco module instead, it's generated by Conan and includes all modules.

    Your installation command is correct:

    conan install . --output-folder=build --build=missing

    Now, we need to build the project:

    $cd build/
    
    $ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake
    -- Using Conan toolchain: /tmp/example/build/conan_toolchain.cmake
    -- Conan toolchain: C++ Standard 17 with extensions OFF
    -- The CXX compiler identification is AppleClang 14.0.3.14030022
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ - skipped
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Conan: Component target declared 'Poco::Foundation'
    -- Conan: Component target declared 'Poco::JSON'
    -- Conan: Component target declared 'Poco::Net'
    -- Conan: Component target declared 'Poco::Redis'
    -- Conan: Component target declared 'Poco::XML'
    -- Conan: Component target declared 'Poco::Crypto'
    -- Conan: Component target declared 'Poco::Data'
    -- Conan: Component target declared 'Poco::DataMySQL'
    -- Conan: Component target declared 'Poco::DataPostgreSQL'
    -- Conan: Component target declared 'Poco::DataSQLite'
    -- Conan: Component target declared 'Poco::Encodings'
    -- Conan: Component target declared 'Poco::JWT'
    -- Conan: Component target declared 'Poco::MongoDB'
    -- Conan: Component target declared 'Poco::Util'
    -- Conan: Component target declared 'Poco::Zip'
    -- Conan: Component target declared 'Poco::ActiveRecord'
    -- Conan: Component target declared 'Poco::NetSSL'
    -- Conan: Target declared 'Poco::Poco'
    -- Conan: Component target declared 'PCRE2::8BIT'
    -- Conan: Component target declared 'PCRE2::POSIX'
    -- Conan: Component target declared 'PCRE2::16BIT'
    -- Conan: Component target declared 'PCRE2::32BIT'
    -- Conan: Target declared 'pcre2::pcre2'
    -- Conan: Target declared 'BZip2::BZip2'
    -- Conan: Including build module from '/Users/conan/.conan2/p/bzip2da06f0ccd4faa/p/lib/cmake/conan-official-bzip2-variables.cmake'
    -- Conan: Target declared 'ZLIB::ZLIB'
    -- Conan: Target declared 'expat::expat'
    -- Conan: Component target declared 'SQLite::SQLite3'
    -- Conan: Component target declared 'libpq::pgport'
    -- Conan: Component target declared 'libpq::pgcommon'
    -- Conan: Component target declared 'libpq::pq'
    -- Conan: Target declared 'PostgreSQL::PostgreSQL'
    -- Conan: Target declared 'libmysqlclient::libmysqlclient'
    -- Conan: Component target declared 'OpenSSL::Crypto'
    -- Conan: Component target declared 'OpenSSL::SSL'
    -- Conan: Target declared 'openssl::openssl'
    -- Conan: Including build module from '/Users/conan/.conan2/p/opens33d41aee1f477/p/lib/cmake/conan-official-openssl-variables.cmake'
    -- Conan: Component target declared 'zstd::libzstd_static'
    -- Conan: Target declared 'LZ4::lz4_static'
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /tmp/example/build
    
    $ cmake --build .
    [ 50%] Building CXX object CMakeFiles/TestMailPrj.dir/main.cpp.o
    [100%] Linking CXX executable TestMailPrj
    [100%] Built target TestMailPrj
    
    
    $ ./TestMailPrj 
    c3fcd3d76192e4007dfb496cca67e13b
    

    It's important to note two things here:

    For further learning, I recommend you reading the Build a simple CMake project using Conan tutorial.