Gpiod.h can't be found on a newly created project

Hi!

Hardware is Verdin imx8mm.

Using the newest VS Code and Torizon IDE Extension v2.8.1. (downlaoded today).
App is cmake C++.

Freshly installed both, created a new WSL distribution with Torizon Extension.
Tried this with single container and multi container project, created from Torizon Extension.

Added needed libs for gpio, torizonPackages.json:

{
  "prodRuntimeDeps": [
    "libgpiod2"
  ],
  "devRuntimeDeps": [
    "libgpiod2",
    "libgpiod-dev",
    "build-essential",
    "pkg-config"
  ],
  "buildDeps": [
    
  ]
}

Dockerfile.debug has them:

# automate for torizonPackages.json
RUN apt-get -q -y update && \
    apt-get -q -y install \
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
    # __torizon_packages_dev_start__
	    libgpiod-dev:arm64 \
	    build-essential:arm64 \
	    pkg-config:arm64 \
    # __torizon_packages_dev_end__
# DO NOT REMOVE THIS LABEL: this is used for VS Code automation
    && \
    apt-get clean && apt-get autoremove && \
    rm -rf /var/lib/apt/lists/*

#include <gpiod.h> is missing.
Code can’t compile, this is my main.c:

#include <iostream>
#include <gpiod.h>

int main() {
    std::cout << "Hello Torizon!" << std::endl;

    return 0;
}

– Configuring done
– Generating done
– Build files have been written to: /home/torizon/app/build-arm64

    • Terminal will be reused by tasks, press any key to close it. *
    • Executing task in folder Embedded: DOCKER_HOST= docker run --rm -v /home/mb/FedMultiContrainerTest/Embedded:/home/torizon/app cross-toolchain-arm64-embedded cmake --build build-arm64 *

[ 50%] Building CXX object CMakeFiles/Embedded.dir/src/main.cpp.o
/home/torizon/app/src/main.cpp:2:10: fatal error: user/include/gpiod.h: No such file or directory

  • 2 | #include <user/include/gpiod.h>*
  •  |          ^~~~~~~~~~~~~~~~~~~~~~*
    

compilation terminated.
gmake[2]: *** [CMakeFiles/Embedded.dir/build.make:76: CMakeFiles/Embedded.dir/src/main.cpp.o] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/Embedded.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
exit status 2

Is there anything else I must do or setup for a cmake C++ application?

Thank you!

Br,
Mladen

Hi, I’m the Toradex AI Assistant. The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Hello!

To use GPIO in your C/C++ project on a Toradex platform with Torizon, you need to make sure the necessary packages are included and the library is properly linked. Here’s how you can do it:

  1. Add Required Packages: Update your torizonPackages.json to include the necessary GPIO packages:

    {
        "deps": [
            "libgpiod2"
        ],
        "devDeps": [
            "libgpiod-dev"
        ]
    }
    

    This ensures that the GPIO library is available in your development environment.

  2. Link the Library: If you were using a Makefile, you would add the library to LDFLAGS like this:

    CC := gcc
    CCFLAGS := -Iincludes/
    DBGFLAGS := -g
    - LDFLAGS :=
    + LDFLAGS := -lgpiod
    CCOBJFLAGS := $(CCFLAGS) -c
    ARCH :=
    

Since you are using CMake, you can achieve the same by modifying your CMakeLists.txt file. Here is a general approach:

  • Find and Link the Library in CMake:
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(GPIOD REQUIRED libgpiod)
    
    include_directories(${GPIOD_INCLUDE_DIRS})
    link_directories(${GPIOD_LIBRARY_DIRS})
    
    add_executable(your_executable_name your_source_files)
    target_link_libraries(your_executable_name ${GPIOD_LIBRARIES})
    

This CMake configuration will find the libgpiod package and link it to your project, allowing you to use GPIO functions in your application.

I hope this helps you set up your project! If you have any more questions, feel free to ask.

Best regards,
Toradex AI Assistant.

Doing what “AI” told me gives another errors(which I’ve also tried before):

1 warning found (use docker --debug to expand):

  • InvalidDefaultArgInFrom: Default value for ARG torizon/debian-cross-toolchain-${IMAGE_ARCH}:${CROSS_SDK_BASE_TAG} results in empty or invalid base image name (line 16)
  • Terminal will be reused by tasks, press any key to close it.

  • Executing task in folder Embedded: DOCKER_HOST= docker run --rm -v /home/mb/FedMultiContrainerTest/Embedded:/home/torizon/app cross-toolchain-arm64-embedded cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -Bbuild-arm64

CMake Error at /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
Call Stack (most recent call first):
/usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.25/Modules/FindPkgConfig.cmake:99 (find_package_handle_standard_args)
CMakeLists.txt:9 (find_package)


It’s interesting that the tutorial doesn’t mention that you need to configure the CMakeLists.txt file for each library you want to add to your project.

Thank you!

Greetings @mbozic,

Just wanted to check to make sure I understand where you are currently at.

Have you now added libgpiod-dev to buildDeps in torizonPackages.json?

Furthermore, did you modify your CMakeLists.txt file to include and link the gpiod library?

Best Regards,
Jeremias

I did this:

find_package(PkgConfig REQUIRED)
pkg_check_modules(GPIOD REQUIRED libgpiod)

include_directories(${GPIOD_INCLUDE_DIRS})
link_directories(${GPIOD_LIBRARY_DIRS})

add_executable(your_executable_name your_source_files)
target_link_libraries(your_executable_name ${GPIOD_LIBRARIES})

And it does not work.

In the meantime I also did this:
“sudo apt install libgpiod-dev”

So libgpio is installed on my WSL2 Linux host.

I also did this too on my host:
“sudo apt install pkg-config”

But the error remains:

 - InvalidDefaultArgInFrom: Default value for ARG torizon/debian-cross-toolchain-${IMAGE_ARCH}:${CROSS_SDK_BASE_TAG} results in empty or invalid base image name (line 16)
 *  Terminal will be reused by tasks, press any key to close it. 

 *  Executing task in folder Embedded: DOCKER_HOST= docker run --rm -v /home/mb/FedMultiContrainerTest/Embedded:/home/torizon/app cross-toolchain-arm64-embedded cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -Bbuild-arm64 

CMake Error at /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:230 (message):
  Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE)
Call Stack (most recent call first):
  /usr/share/cmake-3.25/Modules/FindPackageHandleStandardArgs.cmake:600 (_FPHSA_FAILURE_MESSAGE)
  /usr/share/cmake-3.25/Modules/FindPkgConfig.cmake:99 (find_package_handle_standard_args)
  CMakeLists.txt:10 (find_package)


-- Configuring incomplete, errors occurred!
See also "/home/torizon/app/build-arm64/CMakeFiles/CMakeOutput.log".

 *  The terminal process "/bin/bash '-c', 'DOCKER_HOST= docker run --rm -v /home/mb/FedMultiContrainerTest/Embedded:/home/torizon/app cross-toolchain-arm64-embedded cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -Bbuild-arm64'" terminated with exit code: 1.

This is all I had to do on my side and it compiled successfully:

main.cpp

#include <iostream>
#include <gpiod.h>

int main() {
    std::cout << "Hello Torizon!" << std::endl;

    return 0;
}

torizonPackages.json

{
  "prodRuntimeDeps": [
    "libgpiod2"
  ],
  "devRuntimeDeps": [
    "libgpiod-dev"
  ],
  "buildDeps": [
    "libgpiod-dev"
  ]
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.0.0)

project(test VERSION 1.0 LANGUAGES CXX C)

add_executable(test src/main.cpp)

target_link_libraries(test gpiod)

set(CMAKE_SYSTEM_NAME Linux)

set_target_properties(test PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "bin"
    ARCHIVE_OUTPUT_DIRECTORY "lib"
    LIBRARY_OUTPUT_DIRECTORY "lib"
)

Best Regards,
Jeremias

It compiles!

Thank you!!!

I understand this:
target_link_libraries(test gpiod)

But why did you add it in torizonPackages.json at “buildDeps”?

"buildDeps": [
    "libgpiod-dev"
  ]

I’m pretty new in all of this, is there a tutorial or documentation how this should be done properly?
As example, I will also need to use I2C, SPI, ADC, PWM…
How to setup those?
Where to find info about it?

Thank you!

Br,
Mladen

Please reference the following article here: Add Packages, Libraries, Tools and Files | Toradex Developer Center

It shows you what each field in torizonPackages.json affects. We add libgpiod-dev to the SDK container, because the SDK container is what compiles your application. To compile something you need the development libraries, therefore we add the development libraries to the SDK.

As example, I will also need to use I2C, SPI, ADC, PWM…
How to setup those?
Where to find info about it?

Same principal. What development libraries and packages do you need to do these? Once you have them add it to the SDK container, and link the library. It’s very similar to what you’d do when compiling such C/C++ applications manually without VSCode.

If you have further questions on different topics, I would open a new thread just to avoid this thread becoming long and covering several different topics.

Best Regards,
Jeremias