Search code examples
c++cmakepackage-managersconan

Using conan packages in CMake: Library 'mylibrary.a' not found in package


I am using cmake to build my project. I have a library, mylibrary, which is a dependency of my project. mylibrary is packaged with conan. I use the conan CMakeDeps and CMakeToolchain Generators when packaging mylibrary. This is the package_info function of mylibrary's conanfile:

    def package_info(self):

        self.cpp_info.set_property("cmake_find_mode", "config")
        self.cpp_info.set_property("cmake_file_name", "Mylibrary")
        self.cpp_info.components["libmylibrary"].set_property("cmake_target_name", "Mylibrary::Mylibrary")

        self.cpp_info.components["libmylibrary"].libs = ["mylibrary.a"]
        self.cpp_info.components["libmylibrary"].requires = ["gtest::gtest"]

My library is a shared library with the file name libmylibrary.a.I can package the library without having any problems. The find_package call in my projects CMakeLists.txt file looks like this:

find_package(Mylibrary REQUIRED HINTS ${LLIB_DIR})

When I build my project, CMake does declare my library's target, which is mylibrary::mylibrary. But right I run cmake, I get this error:

CMake Error at MyProject/cmakedeps_macros.cmake:4 (message):
Library 'mylibrary.a' not found in package.  If 'mylibrary.a' is a system library,
declare it with 'cpp_info.system_libs' property
Call Stack (most recent call first):
MyProjectLibs/cmakedeps_macros.cmake:48 (conan_message)
MyProjectLibs/Mylibrary-Target-release.cmake:21 (conan_package_library_targets)
MyProjectLibs/MylibraryTargets.cmake:28 (include)
MyProjectLibs/MylibraryConfig.cmake:11 (include)
CMakeLists.txt:196 (find_package)

I am new to cmake's targets and I don't know what to do. I tried using uppercases or lowercases names when calling find_library, but it is not working. I suspect that I wrote something wrong in the package_info method.


Solution

  • So, I did it. I don't understand why or how, but to solve my problem, I had to change this line:

    self.cpp_info.components["libmylibrary"].libs = ["mylibrary.a"]
    

    to this:

    self.cpp_info.components["libmylibrary"].libs = ["mylibrary"]