1

I need to link the c++ bindings of libgpiod to my project, but I cannot figure out how to do this. I did some research the last few days but without any luck and now I'm kinda stuck.

I would like to add the repo as a submodule, build the module and then link against the module so that I can move the generated binaries (including the module binaries) to the target device.

I cannot simply install the library via a packet manager, because the devices, on which the library should be installed, has no internet access. I could download the package and ship it with the binaries (my current best solution), but I would prefer a "self-containing" build.


My project structure looks roughly as following:

dependencies/
├─ libgpiod/
CMakeLists.txt
main.cpp

and my CMakeLists looks like this:

cmake_minimum_required(VERSION 3.16.3)
project(my_project VERSION 0.1.0)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)


set(GPIOD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/libgpiod)
set(GPIOD_BIN ${CMAKE_CURRENT_BINARY_DIR}/dependencies/libgpiod)
set(GPIOD_LIB_DIR ${GPIOD_BIN}/lib)
set(GPIOD_INCLUDE_DIR ${GPIOD_BIN}/include)
ExternalProject_Add(libgpiod-project
        PREFIX ${GPIOD_BIN}
        SOURCE_DIR ${GPIOD_DIR}
        CONFIGURE_COMMAND ${GPIOD_DIR}/autogen.sh --enable-tools=no --prefix=${GPIOD_BIN} --enable-bindings-cxx
        BUILD_COMMAND make && make install
        # INSTALL_COMMAND make install
        BUILD_BYPRODUCTS ${GPIOD_LIB_DIR}/libgpiodcxx.so
    )



add_executable(my_project main.cpp)

target_link_libraries(my_project PRIVATE
    # todo what to link against?
)

The source code is also available on GitHub, if you need a closer look: https://github.com/mobergmann/led-cube/tree/9e8c7bc4d0de2e8c796a30d19f903c23903cd5bd/cxx

Moritz
  • 378
  • 5
  • 14
  • It looks like gpiod doesn't have CMake support. [see this CMake Discource thread that looks relevant](https://discourse.cmake.org/t/external-project-using-makefile/2692/2). – starball Sep 03 '22 at 23:04
  • You got it wrong, i.e. Unix way of splitting software is to make dependencies on the higher (not in your own repository) level. You need to add `libgpiod` as a compile time / link time dependency and there are plenty of examples for the CMake. I.o.w. just add it like any other library. Keeping the submodule is quite a bad idea (if you don't understand why, you will, if you are doing that on purpose, then you on your own). – 0andriy Sep 05 '22 at 09:23

0 Answers0