Search code examples
c++cmakebuilddependenciesdependency-management

How to satisfy find_package() inside CMakeLists.txt?


I am trying to use sdbus-cpp library. To use the documentation says you have to use find_package(). I undestand that this command searches for sdbus-c++ on the user's system.

I, however, want this library to be built together with my own source. Thus the question: how do I satisfy the find_package() inside my own CMakeLists.txt?

The library author does provide instructions on how to build and install the library, but I don't the use of my own library to be forced to type extra commands in the terminal. I guess I have to add Find*.txt file or add install() command but I couldn't make them work...


Solution

  • You can use FetchContent for this. For example:

    cmake_minimum_required(VERSION 3.15)
    project(TestSdbus)
    
    include(FetchContent)
    FetchContent_Declare(sdbus
      GIT_REPOSITORY https://github.com/Kistler-Group/sdbus-cpp.git
      GIT_TAG        master
    )
    option(BUILD_SHARED_LIBS "" ON)
    option(BUILD_EXAMPLES "" OFF)
    FetchContent_MakeAvailable(sdbus)
    
    set(CMAKE_CXX_STANDARD 17)
    set(SOURCES main.cpp)
    add_executable(test_sdbus ${SOURCES})
    target_link_libraries(test_sdbus sdbus-c++)
    

    Note that this library has an external dependency on pkj-config and libsystemd-dev system packages.