I am new to Conan, and I'm struggling to use specific conan packages in CMake.
I started with including boost to my project using conan, and that worked out great out-of-the-box. the project compiled and linked successfully from the start. I then tried adding cli11, and the project configures and generates fine in cmake, but I can't get it to compile: include headers are not found.
Here's my conanfile.txt:
[requires]
boost/1.79.0
cli11/2.2.0
[generators]
cmake_find_package_multi
# CMakeDeps
# CMakeToolchain
Here's my very basic cmake file:
#set CMAKE_MODULE_PATH to find cmake files generated by conan in build folder
set(CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR} ${CMAKE_MODULE_PATH})
set(CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR} ${CMAKE_PREFIX_PATH})
find_package(Boost REQUIRED)
find_package(CLI11 REQUIRED)
add_executable(myproject main.cpp)
target_link_libraries(myproject
PRIVATE
Boost::Boost
CLI11::CLI11
)
And here's how I call the whole thing:
$ mkdir build && cd build
$ conan install .. --build=missing
$ cmake ..
$ cmake --build .
Finally, here's my compile output:
-- Generating done
-- Build files have been written to: /home/user/dev/myproject/build
> ninja
[1/2] Building CXX object CMakeFiles/myproject.dir/Debug/main.o
FAILED: CMakeFiles/myproject.dir/Debug/main.o
/usr/bin/c++ -DCMAKE_INTDIR=\"Debug\" -g -MD -MT CMakeFiles/myproject.dir/Debug/main.o -MF CMakeFiles/myproject.dir/Debug/main.o.d -o CMakeFiles/myproject.dir/Debug/main.o -c /home/user/dev/myproject/main.cpp
/home/user/dev/myproject/main.cpp:2:10: fatal error: CLI/CLI.hpp: No such file or directory
2 | #include <CLI/CLI.hpp>
| ^~~~~~~~~~~~~
compilation terminated.
ninja: build stopped: subcommand failed.
From the documentation for conan's cli11 package, I see that they are using different cmake generators than me, namely CMakeDeps and CMakeToolchain, and then invoke cmake by passing a conan-generated toolchain file. I tried that too, but it broke linking, as now ld can't find Boost. In addition it also didn't fix the compile issue for cli11 (if I comment out boost from the project I still get the "file not found" issue I had before...)
-- Generating done
-- Build files have been written to: /home/user/dev/myproject/build
[1/1] Linking CXX executable Debug/myproject
FAILED: Debug/myproject
: && /usr/bin/c++ -m64 -g -m64 -rdynamic CMakeFiles/myproject.dir/Debug/main.o -o Debug/myproject -lboost::boost && :
/usr/bin/ld: cannot find -lboost::boost
collect2: error: ld returned 1 exit status
I thought this might be a bug from cli11 so I tried other libs (fmt, spdlog...) and all of them fail to provide me with the library's includes during compilation.
I tried deleting my conan cache with conan remove "*"
multiple times, tried different cmake generators, etc nothing helps.
What's even weirder is that if I take a look inside the generated cli11
target's properties such as INTERFACE_INCLUDE_DIRECTORIES
, my include dirs are present. they just aren't passed to my executable when I target_link_libraries
them. I tried explicitly passing them to target_include_directories
using get_property
, but that didn't work out either.
At this point I guess I am too much of a noob with both conan and CMake to figure it out.
Could someone help me out?
I am using conan version 1.48.0 with cmake 3.21.1 on ubuntu 20.04