Search code examples
cmakefmt

Issue on using {fmt} as a dependency with CMake


I'm trying to apply modern CMake practices in my project. And I've came up with an issue with the {fmt} library dependency.

The structure of a project is the following (in a brief):

dev/
|
+--- fmt/   *unpacked archive of 4.1.0 version*
|
+--- mylib/
|    |
|    +--- mylib.hpp
|    |
|    +--- CMakeLists.txt
|         ***************************
|         * ...
|         * add_library(mylib INTERFACE)
|         * TARGET_LINK_LIBRARIES(mylib PUBLIC fmt-header-only)
|         * set(MYLIB_HEADERS_ALL mylib.hpp )
|         * ...
|         ***************************
|
+--- sample/
|    |
|    +--- main.cpp
|    |
|    +--- CMakeLists.txt
|         ***************************
|         * set(SAMPLE sample.hello_world)
|         * add_executable(${SAMPLE} main.cpp)
|         * TARGET_LINK_LIBRARIES(${SAMPLE} PRIVATE mylib)
|         * install(TARGETS ${SAMPLE} DESTINATION bin)
|         ***************************
|
+--- CMakeLists.txt
     ***************************
     * include_directories(${CMAKE_CURRENT_SOURCE_DIR})
     * add_subdirectory(fmt EXCLUDE_FROM_ALL)
     * add_subdirectory(sample/hello_world)
     ***************************

When I try to build it I receive an error:

PATH/mylib/mylib.hpp:6:10: fatal error: fmt/format.hpp: No such file or directory
 #include <fmt/format.hpp>
          ^~~~~~~~~~~~~~~~
compilation terminated.

Full reproduction can be found here: https://bitbucket.org/ngrodzitski/cmake-issue-fmt-20180410

Any suggestions on the issue?


Solution

  • With the help from Mathieu Ropert in slack, I've resolved the issue in following steps:

    1. TARGET_LINK_LIBRARIES(mylib INTERFACE fmt::fmt-header-only) in mylib/CMakeLists.txt (PUBLIC before).
    2. add the following to root CMakeLists.txt: add_subdirectory(mylib) (that what makes a change).

    I pushed final version to repo: https://bitbucket.org/ngrodzitski/cmake-issue-fmt-20180410.