2

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

lagarkane
  • 915
  • 2
  • 9
  • 22
  • Alright... I took a nap and came back with a fresher mind... I actually found out how to get it all to compile correctly: I'm simply ignoring the CMakeToolchain generator. with only the CMakeDeps generator in my conanfile.txt I can get everything to work OK. But then.. Why does the conan doc tell me to use CMakeDeps with CMakeToolchain? Is this an error in the documentation or am I missing something? – lagarkane May 14 '22 at 13:05
  • The code that you are posting above is from ``cmake_find_package`` integration, not from the ``CMakeDeps`` and ``CMakeToolchain`` one (those are the new integrations, which are Conan 2.0 ready). Probably it is worth to report it to https://github.com/conan-io/conan with updated details that use both CMakeDeps and CMakeToolchain – drodri May 17 '22 at 08:11

1 Answers1

0

There might be a mismatch between conan's build-type and CMake's build-type.

Your conan profile by default specifies build_type as Release and CMake uses Debug as its default CMAKE_BUILD_TYPE.

I found it helpful to always specify the build type. So when compiling for RelWithDebInfo:

mkdir build && cd build
conan install .. --build=missing -s build_type=RelWithDebInfo
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo ..
cmake --build .

If you're on Windows and using MSVC, you should also explicitly set the runtime type using -s compiler.runtime_type=Release for Release/RelWithDebInfo/MinSizeRel builds and -s compiler.runtime_type=Debug for Debug builds. Some tools like Qt's windeployqt rely on this.

Nerix
  • 131
  • 1
  • 3