Library -lgpiod not loaded when using libgpiod, error in VSC torizon tool?

Good morning,

PROBLEM:

I encountered an error following this tutorial:

Once I did all the steps and tried to compile I got this error:

$ arm-linux-gnueabihf-gcc -o testgpio testgpio.o -g 
/usr/lib/gcc-cross/arm-linux-gnueabihf/10/../../../../arm-linux-gnueabihf/bin/ld: testgpio.o: in function `main':
testgpio.c:(.text+0x16): undefined reference to `gpiod_chip_open_by_number'
/usr/lib/gcc-cross/arm-linux-gnueabihf/10/../../../../arm-linux-gnueabihf/bin/ld: testgpio.c:(.text+0x2c): undefined reference to `gpiod_chip_get_line'
/usr/lib/gcc-cross/arm-linux-gnueabihf/10/../../../../arm-linux-gnueabihf/bin/ld: testgpio.c:(.text+0x46): undefined reference to `gpiod_line_request_output'
/usr/lib/gcc-cross/arm-linux-gnueabihf/10/../../../../arm-linux-gnueabihf/bin/ld: testgpio.c:(.text+0x54): undefined reference to `gpiod_line_set_value'
/usr/lib/gcc-cross/arm-linux-gnueabihf/10/../../../../arm-linux-gnueabihf/bin/ld: testgpio.c:(.text+0x62): undefined reference to `gpiod_line_set_value'
collect2: error: ld returned 1 exit status
make: *** [Makefile:8: testgpio] Error 1
The terminal process "/bin/bash '-c', 'make'" terminated with exit code: 2.

DEBUG:

It seems that the library is not loaded during the compilation, indeed the generated Makefile runs:

arm-linux-gnueabihf-gcc -o testgpio testgpio.o -g

TEMP FIX:

The first thing that I tried was to check tasks.json and it was not updated with the option I defined from the GUI by adding devpackages, buildcommands and extrapackages; The only field correctly updated was devpackages, I had to manually write in tasks.json the field buildcommands and extrapackages.

Moreover, to fix this error, I added “-lgpiod” under CFLAGS inside tasks.json such that my debug task looks like:

{
            "label": "build_debug",
            "command": "make",
            "type": "shell",
            "args": [],
            "problemMatcher": {
                "base": "$gcc"
            },
            "options": {
                "env": {
                    "CFLAGS": "-g -lgpiod",
                    "CXXFLAGS": "-g -lgpiod"
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }

I do not know much about visual studio code and how it handles this stuff, but it seems that there is something not working in the Visual studio code addons for Torizon projects.

ANOTHER PROBLEM

I am now following this tutorial: Qt on Torizon OS | Toradex Developer Center

And I would like to add the gpio handling stuff also here, I tried the same approach as described in the previous tutorial but I got stuck into the same error, even if I add the library to the task.json file, it is ignored when the makefile is generated.
My Makefile make command runs:

arm-linux-gnueabihf-g++  -o appconfig_0/work/testapp/testapp testapp.o qrc_qml.o   /usr/lib/arm-linux-gnueabihf/libQt5Quick.so /usr/lib/arm-linux-gnueabihf/libQt5Gui.so /usr/lib/arm-linux-gnueabihf/libQt5QmlModels.so /usr/lib/arm-linux-gnueabihf/libQt5Qml.so /usr/lib/arm-linux-gnueabihf/libQt5Network.so /usr/lib/arm-linux-gnueabihf/libQt5Core.so -lGLESv2 -lpthread  

Even if my tasks.json is:

"tasks": [
        {
            "label": "qmake_debug",
            "command": "${env:QMAKE}",
            "type": "shell",
            "args": [
                "CONFIG+=debug"
            ],
            "problemMatcher": {
                "base": "$gcc"
            },
            "options": {
                "env": {
                    "QMAKE_DESTIDIR": "${command:torizon.ccpp.getTargetFolder}",
                    "CFLAGS": "-g -lgpiod",
                    "CXXFLAGS": "-g -lgpiod"
                }
            }
        },
        {
            "label": "build_debug",
            "command": "make",
            "type": "shell",
            "args": [],
            "problemMatcher": {
                "base": "$gcc"
            },
            "options": {
                "env": {
                    "CFLAGS": "-g -lgpiod",
                    "CXXFLAGS": "-g -lgpiod"
                }
            },
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": [
                "qmake_debug"
            ]
        },
....

EDIT:
TEMP SOLUTION TO SECOND PROBLEM
I just got that the makefile is generated by Qmake and it is possible to assign some parameters from args in tasks.json as it is done with “CONFIG+=debug”. I just added “LIBS += -lgpiod” and now the generated makefile contains that library too.
This is a temporary fix and I believe that this issue needs to be solved by the tool

Someone can explain to me where I can understand a bit more on this stuff?

Greetings @zobeer,

For your first issue, it’s stated in the article you’re referencing that for the example to compile properly you’ll need to add a reference in the Makefile to libgpiod: C/C++ Development and Debugging on TorizonCore Using Visual Studio Code | Toradex Developer Center

What you did in tasks.json is essentially the same since CFLAGS and CXXFLAGS are evaluated in the Makefile.

Your second issue is somewhat related to the first but now it’s more on the Qt side of things. Qt applications as you’ve noticed use Qmake rather than standard make for compilation. Since this is Qt stuff we don’t really document the usage of Qmake ourselves so the best would be to look at the official Qt documentation: Running qmake | qmake Manual

Best Regards,
Jeremias

Hi @zobeer,

Thanks for the detailed writeup. I think what you are asking for is documented here. Basically you use the Torizon extension configuration tab to add extra libraries and such without having to manually edit the JSON files. Please review that and let me know if I am misunderstanding your issue.

Drew

Hi Drew,
Yes I was using that tool but more than once it missed updating the task.json file and I had to do it manually.

Kind regards
Z

Hi @zobeer,

Just wanted to check as it was unclear, are you still having issues here or is this resolved now?

Best Regards,
Jeremias

Hi,
I solved the problem manually, but the tool isn’t working properly on my setup, I will try to reinstall it on vsc but it might be a bug:
When I modify devpackages, buildcommands and extrapackages from the tool provided for vsc, it does not update the task.json file.
Kind regards
Zobeer

I believe you should be looking at the config.yaml file to see these changes. All properties in the configuration view of the extension should be reflected in that project’s config.yaml file. Then I believe the project uses this to get the defined packages and commands.

Best Regards,
Jeremias