conan-io / examples2

Conan 2.x examples
MIT License
87 stars 48 forks source link

requirements for lib with components #155

Closed Dimitrius-dev closed 1 month ago

Dimitrius-dev commented 1 month ago

Description:

When i link external lib (like a boost) to my custom lib everything works fine. I tried to do the same with the lib that consists of several components and it did not work. For simplicity i took one of these examples to explain.

I used example of this repo: examples/conanfile/package_info/components

These code was modified a little.

conanfile.py

...
def requirements(self):
        self.requires("boost/1.84.0", transitive_headers=True)
...

network.h

#include <boost/asio/io_context.hpp>
...

network.h

#include <boost/asio/io_context.hpp>
...

CMakeLists.txt

...
find_package(Boost 1.84.0 REQUIRED)

target_link_libraries(ai algorithms boost::boost)
...

The error i received:

In file included from /mnt/m/files/job/temp/test_conan_example/examples/conanfile/package_info/components/test_package/src/example.cpp:2:
/home/belov/.conan2/p/b/game-45b670e1e27fa/p/include/network.h:2:10: fatal error: boost/asio/io_context.hpp: No such file or directory
    2 | #include <boost/asio/io_context.hpp>
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~~~
It is possible that this recipe is not Conan 2.0 ready
If the recipe comes from ConanCenter, report it at https://github.com/conan-io/conan-center-index/issues
If it is your recipe, check if it is updated to 2.0
*********************************************************

ERROR: game-engine/1.0 (test package): Error in build() method, line 23
        cmake.build()
        ConanException: Error 2 while executing

_The file boost/asio/iocontext.hpp is in boost lib.


Question

Should i add some code to conanfile.py to fix this in this case?

memsharded commented 1 month ago

Hi @Dimitrius-dev

Please check the messages that appear when you conan install the dependencies, they will print the find_package() and the target names to use when target_link_libraries().

I think the issue might be a mispelling on the boost target, the correct target name would be Boost::boost the first with uppercase.

Please also note that the example in this page for package_info() is not designed as a base to derive your work, just as a base to explain the concepts in the tutorial. It might be missing some parts like package_type that can be necessary to create a fully usable package when adding other transitive dependencies.

If the target typo above doesn't fix, please try to detail a bit more the exact steps, including the commands you are using to reproduce. Thanks for the feedback

Dimitrius-dev commented 1 month ago

The general target to link boost is boost::boost according official Conancenter repo. Therefore cmake find_package with REQUIRED parameter did not stop the process. I suppose something is blocking inclusion of boost headers to the project. And there is no cmake target that matches with the conanfile project name.

I used command conan create .. And I have already installed by conan boost 1.84.0.

Dimitrius-dev commented 1 month ago

I resolved this case adding lines self.cpp_info.components["network"].requires = ["boost::boost"] and self.requires(transitive_headers=True) in package_info method. I assumed all requirements are open to all targets by default. And it is not, found example in https://github.com/mpusz/mp-units/blob/master/conanfile.py. I really appreciate your time and your help. Thank you)

memsharded commented 1 month ago

Note that the official ConanCenter recipes uses targets such as Boost::xxxx the first with uppercase, see it in https://github.com/conan-io/conan-center-index/blob/efd720ca6e8ac24f62d59ee73a43452d4a832fe0/recipes/boost/all/test_package/CMakeLists.txt#L21, see also https://github.com/conan-io/conan-center-index/blob/efd720ca6e8ac24f62d59ee73a43452d4a832fe0/recipes/boost/all/conanfile.py#L1755. that uses Boost::boost.

The find_package(Boost ...) is correct, it is the target name that could be a problem

self.cpp_info.components["network"].requires = ["boost::boost"]

This is correct, the cpp_info.requires uses the "package" reference, which is always lowercase, this is not the same as the CMake target name.

Thanks for the feedback!

Dimitrius-dev commented 1 month ago

image So does the conancenter boost repo contain invalid boost target in readme file?

And does conan provide general lib target by default. I mean some lib "testlib" has components named ("a", "b"), so conan creates target testlib::testlib which is linked with a, b, c, right?

memsharded commented 1 month ago

Sorry, you were correct.

Boost::boost is an alias of Boost::headers, while boost::boost is the target that aggregates all libraries, including compiled ones, I got confused while reading the test_package/conanfile.py.