Linking libraries with the nRF connect extension

Hello all,

I am a student trying to understand the nRF connect extension on vscode on windows subsystem wsl2. Usually when using gcc to link external libraries and compile code, it is simply using gcc -o main main.c -l<library name> in the terminal. However, on the nRF connect, building and flashing a program onto the nrf52832 board is just a matter of pressing the build project and flash on the vscode interface as shown in the figure below.

Hence I would like to know how do I link libraries on the nRF connect extension on vscode? Thank you.

  • Hi,

    2. Usually when using gcc to link external libraries and compile code, it is simply using gcc -o main main.c -l<library name> in the terminal.

    Yep. However, the nRF Connect SDK uses the Zephyr build system to build stuff, which handles all the building, including the linking. So it is not as straight forward to include an external library.

    To check that this is not an XY Problem: What library do you want to include?

    Zephyr has a sample for including external library: https://developer.nordicsemi.com/nRF_Connect_SDK/doc/latest/zephyr/samples/application_development/external_lib/README.html. That is likely what you are looking for. Its docs are not very extensive, so probably have a look at its source, and it's CMakeLists.txt in particular.

    PS:

    do I link libraries on the nRF connect extension on vscode?

    It is not a VS Code extension question, but nRF Connect SDK and Zephyr question in general, as it is all being handeled in the project files. The VS Code extension does not have any features specifically for linking external libraries.

    Regards,
    Sigurd Hellesvik

  • Hello Sigurd, 

    Thank you for replying. I examined Zephyr's sample for including external libraries. I noticed that the sample external library used, mylib has only 2 files, the source file and the header file as shown:

    However the external library, FFTW3 that I want to include has multiple files within it as shown below. Hence I was not able to fully replicate what was done in the sample project provided by Zephyr including its CMakeLists.txt.

    What steps I have taken

    1. I did manage to find the libfftw3.a in the directory usr/local/lib and the header file fftw3.h in the directory usr/local/include.
    2. I opened a blinky_pwm sample to modify the CMakeLists.txt and to see whether I can use the FFTW3 functions .
    3. I modified the CMakeLists.txt for the blinky_pwm as follows:

    Upon building the blinky_pwm project, this is the error that shows on my terminal:

    Hence I am currently unsure of whether the steps I took so far are in the correct direction and if it is not, what is the correct way to do so?  Thank you.

  • Dayn said:
    Thank you for replying. I examined Zephyr's sample for including external libraries. I noticed that the sample external library used, mylib has only 2 files, the source file and the header file as shown:

    The external_lib sample does two things:

    1. Build the external lib and generate .a file
    2. Link the .a file to your project (and include headers)

    If you do not want FFTW3 to be built at the same time as the application and already have the .a file, you can skip step 1.

    Step 2 would in that case be the following part of the code I think:

    https://github.com/nrfconnect/sdk-zephyr/blob/96325d267d652738e48d0be694f4b703d6912623/samples/application_development/external_lib/CMakeLists.txt#L60-L69

    Dayn said:
    I modified the CMakeLists.txt for the blinky_pwm as follows:

    Instead of what you did there, try to do the same as the lines I link to above.

    Last time I did this was when working with the blogpost for  TensorFlow and nRF . Maybe a quick look at that can help you?

  • Hello, 

    I followed the CMakeLists.txt as per what you have linked as shown below. I only included the portion where it creates a wrapper to link with my app. I removed the add_dependencies() function since I am only trying to link the .a file to my project. 

    However I am still getting the same error when I build the project as shown below:

    I have checked that is not a case of mismatched architecture, incorrect file format or incomplete installation of the external library.

  • Which processor architecture is your .a file built for?

    I found a similar issue from a completely different field, where the error was architecture types.

Related