conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
7.95k stars 951 forks source link

cmake find_package failed when use conanfile.py #16480

Open boboxxd opened 2 weeks ago

boboxxd commented 2 weeks ago

What is your question?

conanfile.py as follows:

from conan import ConanFile
from conan.tools.cmake import CMakeToolchain, CMake, cmake_layout
from conan.tools.files import get
from conan.tools.scm import Git
from conan.tools.files import copy

class MyProjectConan(ConanFile):
    settings = "os", "compiler", "build_type", "arch"
    default_options = {
        "fmt/*:shared": True
    }

    def requirements(self):
        self.requires("workflow/0.11.4")
        self.requires("libuuid/1.0.3")
        self.requires("sqlitecpp/3.3.1")
        self.requires("fmt/10.2.0")
        self.requires("toml11/3.8.1")
        self.requires("handy/0.2.0")
        self.requires("zlib/1.2.11")
        self.requires("paho-mqtt-cpp/1.3.2")

    def layout(self):
        cmake_layout(self)

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

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

cmakelists.txt as follows:

cmake_minimum_required(VERSION 3.20)
set(DEPENDS_LIB "")

find_package(workflow REQUIRED)
if(workflow_FOUND)
    include_directories(${WORKFLOW_INCLUDE_DIR})
    link_directories(${WORKFLOW_LIB_DIR})
    list(PREPEND DEPENDS_LIB workflow)
endif()

find_package(OpenSSL REQUIRED)
if(OPENSSL_FOUND)
    include_directories(${OPENSSL_INCLUDE_DIR})
    list(PREPEND DEPENDS_LIB ${OPENSSL_SSL_LIBRARY})
    list(PREPEND DEPENDS_LIB ${OPENSSL_CRYPTO_LIBRARY})
endif()

# find_package(fmt REQUIRED)
# if(fmt_FOUND)
#     include_directories(${FMT_INCLUDE_DIR})
#     list(PREPEND DEPENDS_LIB fmt::fmt)
# endif()

find_package(PahoMqttCpp REQUIRED)
# ...
# target_link_libraries(YOUR_TARGET PahoMqttCpp::paho-mqttpp3-static)

find_package(ZLIB REQUIRED)
if(ZLIB_FOUND)
    include_directories(${ZLIB_INCLUDE_DIR})
    list(PREPEND DEPENDS_LIB ZLIB::ZLIB)
    message(STATUS ${ZLIB_INCLUDE_DIR})
endif()
# find_package(toml11 REQUIRED)

# find_package(SQLiteCpp REQUIRED)

add_executable(demo main.cpp)
target_link_libraries(demo ${DEPENDS_LIB})

question is find_package(PahoMqttCpp REQUIRED) cannot find. I used the conan v2 image

Have you read the CONTRIBUTING guide?

memsharded commented 2 weeks ago

Hi @boboxxd

Thanks for your report.

I was trying to reproduce but I encountered a few issues:

I avoided those by commenting things out or modifying your CMakeLists.txt.

Then the main issue is that you are missing the CMakeDeps generator. Add to your generate():

    from conan.tools.cmake import CMakeToolchain, CMakeDeps, CMake, cmake_layout
    ...
    def generate(self):
        tc = CMakeToolchain(self)
        tc.generate()
        deps = CMakeDeps(self)
        deps.generate()

And then with something like conan build . you will see in the output:

-- Conan: Component target declared 'PahoMqttCpp::paho-mqttpp3-static'
-- Conan: Component target declared 'eclipse-paho-mqtt-c::paho-mqtt3as-static'
-- Conan: Component target declared 'OpenSSL::Crypto'
-- Conan: Component target declared 'OpenSSL::SSL'
-- Conan: Target declared 'openssl::openssl'
-- Conan: Target declared 'ZLIB::ZLIB'

which means it was found.

Please let me know if this helps.