Libsocketcan2 library functions not recognised in Torizon VSCode extension

How do you use libsocketcan2 library in the Torizon extension for VSCode?
I tried to add it in extrapackages in the configurations tab of the Torizon extension.

In my .cpp file there is a function called from this library called can_set_bitrate() which is defined at the following.
https://lalten.github.io/libsocketcan/Documentation/html/group__extern.html#gaf69a4a0d7424e5a61d60991295f50559

However, when I try to compile inside the remote container, it does not seem to work. Get the following error.

cant2.cpp:18:10: fatal error: libsocketcan.h: No such file or directory
18 | #include <libsocketcan.h>
| ^~~~~~~~~~~~~~~~
compilation terminated.
The terminal process “/bin/bash ‘-c’, ‘aarch64-linux-gnu-g++ -g cant2.cpp -o cant2’” failed to launch (exit code: 1).

In addition, I tried to add this library to the devpackages as well as described at the following link:

However, I am still getting an error that the libsocketcan.h file cannot be opened.

Dear @SJ_BHD, how are you?

On the debian feed (Debian -- Software Packages in "bullseye") there is no libsocketcan2-dev. There is only libsocketcan-dev. Could you please test with it?

Also, which type of C project is this? A CMake?

Best regards,

Hello @gclaudino.tx , I am good . How are you?
Yes with the libsocketcan-dev in devpackages and libsocketcan2 in extra packages, the red squiggles under my .cpp file for the #include <libsocketcan.h> declaration have gone.

However, after running the build task from Terminal → Run build Task (Ctrl+Shift+B), there is still a compilation error for the can_set_bitrate() function as follows. It appears to be a linker error.

Executing task: aarch64-linux-gnu-g++ -g cant2.cpp -o cant2 <

/usr/lib/gcc-cross/aarch64-linux-gnu/10/../../../../aarch64-linux-gnu/bin/ld: /tmp/ccd0ANtR.o: in function `main':
/workspaces/cant2/cant2.cpp:56: undefined reference to `can_set_bitrate'
collect2: error: ld returned 1 exit status
The terminal process "/bin/bash '-c', 'aarch64-linux-gnu-g++ -g cant2.cpp -o cant2'" terminated with exit code: 1.

Dear @SJ_BHD,

Thanks for the update. I’m good too :slight_smile:. Can you please share a bigger extract of your code so that we could see how did you use the can_set_bitrate()? There seems to be something on your code as the library is now loaded.

If you want, you can do this either here on the community directly or at support@toradex.com or through a private message here.

Hello,
I am using the following.

can_set_bitrate("can0",100000);

Dear @SJ_BHD,

Can you please confirm the information above?

If your project uses CMake, for instance, you would need to add the library with something like: target_link_libraries(your_app socketcan) (https://cmake.org/cmake/help/latest/command/target_link_libraries.html).

If you use Makefile, you should add it with something like:
$(CC) -o $@ $^ $(CFLAGS) -lsocketcan (c++ - Adding a library to a makefile - Stack Overflow)

I selected the import from existing C/C++ file option for the Torizon VSCode extension.
I think this is based on the Single C/C++ file option.

Hi @SJ_BHD,

You can add the -lsocketcan command on the tasks.json file. This shall be done both on the “args” of build_debug and build_release. You’ll probably end with something like:

            "args": [
                "-g",
                "cant2.cpp",
                "-o",
                "cant2",
                "-lsocketcan"
            ],

However, I’d like to suggest you use a build framework such as CMake or Makefile for future projects as it can help you with some of the project development.

Best regards,

Hello,

I updated the tasks.json file and then ran the code in a debug container within VSCode and it worked fine.
I also changed to a release configuration and ran the code in a release container within VSCode, it worked fine.

However, when I try to invoke the release container outside VSCode using a command docker run from SSH terminal ( using Putty), the container does not run successfully.

It is invoked but I get an error as below.
apalis-imx8-07013612:~$ docker run cant2_arm64v8-debian-no-ssh_bullseye_release_9462f5c5-4e70-46e8-86a6-bae0e204d3c3
Error in socket bind: No such device

Not able to understand why this is happening, as in within VSCode inside the development container, I can see the program running but not via Putty.

Hi @SJ_BHD,

Thanks for the update. Can you please try running the container on your module using the output that F1 Torizon: export docker command line provides on the extension? You may be missing some of the parameters to run your project properly.

Best regards,

Hi @SJ_BHD, is this working now? Do you have any news on the topic?

Best regards,

Thanks, the container now worked on the command line.

1 Like