Search code examples
c++cmakemakefilecompiler-errorsdependencies

Unable to compile a CMake project which fetches two dependencies and one of them depends on the other, why?


I have the following problem. I am trying to compile a project which depends on two libraries: SFML and Thor, and the latter depends on the former.

In my main CMakeLists.txt I fetch these dependencies with:

...
add_subdirectory(SFML)
add_subdirectory(Thor)
...

The CMakeLists of SFML fetching is the following:

# Finding the package
find_package( SFML 2.5.1 COMPONENTS system window graphics )

# Fetching the package if not found
if( NOT SFML_FOUND )

    # Fetching the package
    include( FetchContent )

    FetchContent_Declare(
      SFML
      GIT_REPOSITORY "https://github.com/SFML/SFML"
      GIT_TAG 2.5.1
    )

    # No need to build audio and network modules
    set( SFML_BUILD_AUDIO FALSE )
    set( SFML_BUILD_NETWORK FALSE )

    # Make it available
    message( STATUS "Fetching SFML..." )
    FetchContent_MakeAvailable( SFML )
endif()

While the one of Thor fetching is this other one:

# Fetching the package
include( FetchContent )

FetchContent_Declare(
  Thor
  GIT_REPOSITORY "https://github.com/Bromeon/Thor"
  GIT_TAG master # 2.0
)

# Make it available
message( STATUS "Fetching Thor..." )
FetchContent_MakeAvailable( Thor )

The problem is that when compiling I got the following error:

CMake Error at build/_deps/thor-src/CMakeLists.txt:114 (find_package):
  By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SFML", but
  CMake did not find one.

  Could not find a package configuration file provided by "SFML" (requested
  version 2.5) with any of the following names:

    SFMLConfig.cmake
    sfml-config.cmake

  Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or set
  "SFML_DIR" to a directory containing one of the above files.  If "SFML"
  provides a separate development package or SDK, be sure it has been
  installed.

Do you have any idea of what is going on? Thanks.


Solution

  • To answer your question very briefly, take a look at the documentation of find_package:

    If the package configuration file cannot be found CMake will 
    generate an error describing the problem unless the QUIET 
    argument is specified. If REQUIRED is specified and the 
    package is not found a fatal error is generated and the configure 
    step stops executing. If <PackageName>_DIR has been set to a directory not 
    containing a configuration file CMake will ignore it and search from scratch.
    

    From this we can deduce that all you need to do is update the find_package call as so:

    find_package( SFML 2.5.1 QUIET COMPONENTS system window graphics )
    

    Hope it helps!

    EDIT: As Tsyvarev pointed out to me, the error comes from the Thor project. My bad for not seeing it early. The solution to your problem is then within the parameters of FetchContent itself. From a certain version (I do not recall which one) it had the optional parameter: OVERRIDE_FIND_PACKAGE you can find the mention of it in this link i.e. what you want to do is actually this:

    FetchContent_Declare( <name> 
      GIT_REPOSITORY <repo_url>
      GIT_TAG        <tag>
      OVERRIDE_FIND_PACKAGE
    )
    

    Big thanks to Tsyvarev for pointing out what I missed!