Search code examples
c++cmakeconanimgui

C++ file cannot find library linked with CMake


I wanted to use DearImGui therefore I needed to either copy ImGui into the project or use a package manager, so I chose the latter. I'm currently using Conan as a my package manager, the file looks like this:
conanfile.txt

[requires]
boost/1.79.0
imgui/1.88
glad/0.1.36
glfw/3.3.7

[generators]
cmake_find_package
cmake_paths
CMakeDeps
CMakeToolchain

It has all the dependancies as a normal ImGui project would need as well as boost which had been downloaded as a binary previously and works fine, using just a normal header like #include <boost/asio.hpp>. I figure this happens because it was aleady installed.

I have a basic file structure that looks like this, prebuild:

conanfile.txt
CMakeLists.txt
src -
    main.cpp

With this main CMakeLists I set up a linker with conan and simple setup instructions like the project name, executable, and the libraries

cmake_minimum_required(VERSION 3.23)
project(RenderCam CXX)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(${CMAKE_CURRENT_SOURCE_DIR}/build/conan_paths.cmake)
add_executable(${PROJECT_NAME} src/main.cpp)

find_package(Boost 1.79.0 REQUIRED COMPONENTS thread system filesystem)

if(TARGET boost::boost)
    target_link_libraries(${PROJECT_NAME} boost::boost)
endif()

find_package(glfw3 3.3 REQUIRED)

if(TARGET glfw)
    message("Found GLFW")
    target_link_libraries(${PROJECT_NAME} glfw)
endif()

find_package(imgui 1.88 REQUIRED)

if(TARGET imgui::imgui)
    message("found imgui")
    target_link_libraries(${PROJECT_NAME} imgui::imgui)
endif()

After running two commands (conan install .. and cmake .. -DCMAKE_BUILD_TYPE=Release) in the new build directory the CMake build responds with all the found messages for the project as well as the location of the files, which should be expected. Though when add the #include <imgui.h> to the main.cpp it states the file is not found which shouldn't be if I have linked it. On the other hand #include <boost/asio.hpp> has no errors and I can go upon my day with all of the commands. I would just like to include the directories and have tried multiple steps and guides before I took it here. For further reference, this is my conanprofile :

Configuration for profile default:

[settings]
os=Macos
os_build=Macos
arch=x86_64
arch_build=x86_64
compiler=apple-clang
compiler.version=13
compiler.libcxx=libc++
build_type=Release
[options]
[conf]
[build_requires]
[env]


Solution

  • Don't mix up mutually exclusive generators like cmake_find_package vs CMakeDeps, or cmake_paths vs CMakeToolchain.

    Here is a basic example of non-intrusive integration of conan:

    conanfile.txt

    [requires]
    boost/1.79.0
    imgui/1.88
    glfw/3.3.7
    
    [generators]
    CMakeToolchain
    CMakeDeps
    

    CMakeLists.txt

    cmake_minimum_required(VERSION 3.15)
    project(RenderCam CXX)
    
    find_package(Boost 1.79.0 REQUIRED thread system filesystem)
    find_package(glfw3 3.3 REQUIRED)
    find_package(imgui 1.88 REQUIRED)
    
    add_executable(${PROJECT_NAME} src/main.cpp)
    target_link_libraries(${PROJECT_NAME} PRIVATE Boost::headers Boost::thread Boost::system Boost::filesystem glfw imgui::imgui)
    target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_14)
    

    Install dependencies, configure & build:

    mkdir build && cd build
    conan install .. -s build_type=Release -b missing
    cmake .. -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake -DCMAKE_BUILD_TYPE=Release
    cmake --build . --config Release