Home > Mobile >  How do I correctly #include a conan package?
How do I correctly #include a conan package?

Time:10-29

I have the following cmake configuration.

  conan_cmake_configure(REQUIRES
                        catch2/2.13.7
                        fmt/6.1.2
                        termcolor/2.0.0
                        date/3.0.1
                        asio/1.20.0
                        tl-expected/20190710
                        GENERATORS cmake_find_package)

...
...
...

find_package(fmt REQUIRED)
include_directories(${fmt_INCLUDE_DIR})
link_libraries(${fmt_LIBRARIES})

find_package(termcolor REQUIRED)
include_directories(${termcolor_INCLUDE_DIR})
link_libraries(${termcolor_LIBRARIES})

find_package(date REQUIRED)
include_directories(${date_INCLUDE_DIR})
link_libraries(${date_LIBRARIES})

find_package(asio REQUIRED)
include_directories(${asio_INCLUDE_DIR})
link_libraries(${asio_LIBRARIES})

find_package(tl-expected REQUIRED)
include_directories(${tl-expected_INCLUDE_DIR})
link_libraries(${tl-expected_LIBRARIES})

I can #include the libs like so:

#include <asio.hpp>
#include <fmt/format.h>

but I don't find any information about how exactly to #include the expected lib. I've tried expected.hpp and the example from https://github.com/hannahwhy/conan-tl-expected/blob/stable/1.0.1/test_package/example.cpp

It's fatal error C1083: No such file or directory

CodePudding user response:

I have not used the conan_cmake_configure wrapper but using targets is the recommended modern CMake approach. The readme from the conan cmake wrapper you're using has a few snippets that should be helpful.

Basically, the cmake_find_package generator will define CMake targets from the packages, and define properties such as include directories, libs, compile definitions etc on each target. Whenever you use target_link_libraries(TargetA TargetB), all transitive dependencies will be handled by CMake without needing to worry about specifying include directories and such (you should use targets for your own project too).

So, linking with fmt is very straightforward (taken from examples on the cmake wrapper repo):

find_package(fmt)

add_executable(main main.cpp)
target_link_libraries(main fmt::fmt)

You can check the CMake config files created by conan if you want to be sure of the targets that are declared by the package.

Edit: Just realized this doesn't answer your question per se, but this makes it a lot easier to understand what went wrong. In your current CMake code, the ${x_LIBRARIES} might just be empty and you are not aware of it directly. With targets, CMake will throw an error if you are trying to link with the wrong package name.

CodePudding user response:

Thanks for your tips. The answer is actually easy (as so often).

cmake converts - into _ when creating the include_directories and link_libaries variables. So this fixed it:

find_package(tl-expected REQUIRED)
include_directories(${tl_expected_INCLUDE_DIR})
link_libraries(${tl_expected_LIBRARIES})
  • Related