How to create a static library on nRF Connect SDK/Zephyr

Hi,

I am using nrF52840 with nRF Connect, Visual Code Studio (Zephyr) 

Is there any guide or a tutorial with an example that shows how to create a static library on nRF Connect SDK/Zephyr?

There are some topics opened related to this, but none of them have the complete solution.

Please if you can explain in detail, it would help everyone a lot. Thanks

  • Hi,

     

    Have you looked at this sample in zephyr?

    https://github.com/nrfconnect/sdk-zephyr/blob/main/samples/application_development/external_lib/

    This sample both builds and links with a static library.

     

    Kind regards,

    Håkon

  • Hello Håkon and Akif,

    I feel like that sample does a lot more than what's necessary to simply include a lib. Wouldn't these lines added to the cmake be enough?

    zephyr_include_directories(src/lib)
    target_sources(app PRIVATE src/lib/libname.c)
    

    I remember having a hard time implementing one my self, and that sample confused me more than it helped.

    Please correct me if I'm wrong, but it works for me!

  • To include a lib and to create a lib are two different operations. The below sample shows both.

    If you want to include a precompiled static library, you can shorten the whole thing, similar to this:

    # SPDX-License-Identifier: Apache-2.0
    
    cmake_minimum_required(VERSION 3.20.0)
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(external_lib)
    
    target_sources(app PRIVATE src/main.c)
    
    include(ExternalProject)
    
    # Add an external project to be able download and build the third
    # party library. In this case downloading is not necessary as it has
    # been committed to the repository.
    
    set(MYLIB_LIB_DIR     ${CMAKE_CURRENT_SOURCE_DIR}/lib)
    set(MYLIB_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mylib/include)
    
    # Create a wrapper CMake library that our app can link with
    add_library(mylib_lib STATIC IMPORTED GLOBAL)
    add_dependencies(
      mylib_lib
      mylib_project
      )
    set_target_properties(mylib_lib PROPERTIES IMPORTED_LOCATION             ${MYLIB_LIB_DIR}/libmylib.a)
    set_target_properties(mylib_lib PROPERTIES INTERFACE_INCLUDE_DIRECTORIES ${MYLIB_INCLUDE_DIR})
    
    target_link_libraries(app PUBLIC mylib_lib)

     

    Kind regards,

    Håkon

  • Actually I couldn't build external_lib and I couldnt find any reliable information related creating a static library. That is why I have opened this ticket.

    Thank you for extra information, I think your information will help me if I can build external_lib. Please see the error I get (example in my workspace):

     *  Executing task: nRF Connect: Generate config nrf52dk_nrf52832 for c:\nordic\myapps\external_lib 
    
    Building external_lib
    west build --build-dir c:\nordic\myapps\external_lib\build c:\nordic\myapps\external_lib --pristine --board nrf52dk_nrf52832 -- -DNCS_TOOLCHAIN_VERSION:STRING="NONE" -DCONFIG_DEBUG_OPTIMIZATIONS=y -DCONFIG_DEBUG_THREAD_INFO=y -DBOARD_ROOT:STRING="c:/nordic/myapps/ledagif/led_ws2812"
    
    -- west build: generating a build system
    Loading Zephyr default modules (Zephyr base).
    -- Application: C:/nordic/myapps/external_lib
    -- Found Python3: C:/ncs/toolchains/v2.1.0/opt/bin/python.exe (found suitable exact version "3.8.2") found components: Interpreter 
    -- Cache files will be written to: C:/ncs/v2.0.2/zephyr/.cache
    -- Zephyr version: 3.0.99 (C:/ncs/v2.0.2/zephyr)
    -- Found west (found suitable version "0.14.0", minimum required is "0.7.1")
    -- Board: nrf52dk_nrf52832
    -- Found host-tools: zephyr 0.14.1 (C:/ncs/toolchains/v2.1.0/opt/zephyr-sdk)
    -- Found dtc: C:/ncs/toolchains/v2.1.0/opt/bin/dtc.exe (found suitable version "1.4.7", minimum required is "1.4.6")
    -- Found toolchain: zephyr 0.14.1 (C:/ncs/toolchains/v2.1.0/opt/zephyr-sdk)
    -- Found BOARD.dts: C:/ncs/v2.0.2/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832.dts
    -- Generated zephyr.dts: C:/nordic/myapps/external_lib/build/zephyr/zephyr.dts
    -- Generated devicetree_unfixed.h: C:/nordic/myapps/external_lib/build/zephyr/include/generated/devicetree_unfixed.h
    -- Generated device_extern.h: C:/nordic/myapps/external_lib/build/zephyr/include/generated/device_extern.h
    -- Including generated dts.cmake file: C:/nordic/myapps/external_lib/build/zephyr/dts.cmake
    Parsing C:/ncs/v2.0.2/zephyr/Kconfig
    Loaded configuration 'C:/ncs/v2.0.2/zephyr/boards/arm/nrf52dk_nrf52832/nrf52dk_nrf52832_defconfig'
    Merged configuration 'C:/nordic/myapps/external_lib/prj.conf'
    Merged configuration 'C:/nordic/myapps/external_lib/build/zephyr/misc/generated/extra_kconfig_options.conf'
    Configuration saved to 'C:/nordic/myapps/external_lib/build/zephyr/.config'
    Kconfig header saved to 'C:/nordic/myapps/external_lib/build/zephyr/include/generated/autoconf.h'
    -- The C compiler identification is GNU 10.3.0
    -- The CXX compiler identification is GNU 10.3.0
    -- The ASM compiler identification is GNU
    -- Found assembler: C:/ncs/toolchains/v2.1.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc.exe
    -- Configuring done
    -- Generating done
    -- Build files have been written to: C:/nordic/myapps/external_lib/build
    -- west build: building application
    [1/173] Generating include/generated/version.h
    -- Zephyr version: 3.0.99 (C:/ncs/v2.0.2/zephyr), build: v3.0.99-ncs1-1
    [2/173] Creating directories for 'mylib_project'
    [3/173] No download step for 'mylib_project'
    [4/173] No update step for 'mylib_project'
    [5/173] No patch step for 'mylib_project'
    [6/173] No configure step for 'mylib_project'
    [7/173] Performing build step for 'mylib_project'
    FAILED: mylib/src/mylib_project-stamp/mylib_project-build mylib/lib/libmylib.a 
    cmd.exe /C "cd /D C:\nordic\myapps\external_lib\mylib && make PREFIX=C:/nordic/myapps/external_lib/build/mylib CC=C:/ncs/toolchains/v2.1.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc.exe AR=C:/ncs/toolchains/v2.1.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-ar.exe "CFLAGS=-IC:/ncs/v2.0.2/zephyr/include/zephyr -IC:/ncs/v2.0.2/zephyr/include -IC:/nordic/myapps/external_lib/build/zephyr/include/generated -IC:/ncs/v2.0.2/zephyr/soc/arm/nordic_nrf/nrf52 -IC:/ncs/v2.0.2/zephyr/lib/libc/minimal/include -IC:/ncs/v2.0.2/zephyr/soc/arm/nordic_nrf/common/. -IC:/ncs/v2.0.2/nrf/include -IC:/ncs/v2.0.2/modules/hal/cmsis/CMSIS/Core/Include -IC:/ncs/v2.0.2/modules/hal/nordic/nrfx -IC:/ncs/v2.0.2/modules/hal/nordic/nrfx/drivers/include -IC:/ncs/v2.0.2/modules/hal/nordic/nrfx/mdk -IC:/ncs/v2.0.2/zephyr/modules/hal_nordic/nrfx/. -IC:/ncs/v2.0.2/modules/debug/segger/SEGGER -IC:/ncs/v2.0.2/modules/debug/segger/Config -IC:/ncs/v2.0.2/zephyr/modules/segger/. -Ic:/ncs/toolchains/v2.1.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/include -Ic:/ncs/toolchains/v2.1.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/include-fixed -DKERNEL -D__ZEPHYR__=1 -D_FORTIFY_SOURCE=2 -DUSE_PARTITION_MANAGER=0 -D__PROGRAM_START -DNRF52832_XXAA -DNRF52832_XXAA -Og -imacros C:/nordic/myapps/external_lib/build/zephyr/include/generated/autoconf.h -ffreestanding -fno-common -g -gdwarf-4 -fdiagnostics-color=always -mcpu=cortex-m4 -mthumb -mabi=aapcs -mfp16-format=ieee -imacros C:/ncs/v2.0.2/zephyr/include/zephyr/toolchain/zephyr_stdint.h -Wall -Wformat -Wformat-security -Wno-format-zero-length -Wno-main -Wno-pointer-sign -Wpointer-arith -Wexpansion-to-defined -Wno-unused-but-set-variable -Werror=implicit-int -fno-asynchronous-unwind-tables -fno-pie -fno-pic -fno-reorder-functions -fno-defer-pop -fmacro-prefix-map=C:/nordic/myapps/external_lib=CMAKE_SOURCE_DIR -fmacro-prefix-map=C:/ncs/v2.0.2/zephyr=ZEPHYR_BASE -fmacro-prefix-map=C:/ncs/v2.0.2=WEST_TOPDIR -ffunction-sections -fdata-sections -std=c99 -nostdinc -isystemC:/ncs/v2.0.2/zephyr/lib/libc/minimal/include -isystemc:/ncs/toolchains/v2.1.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/include -isystemc:/ncs/toolchains/v2.1.0/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/10.3.0/include-fixed" && C:\ncs\toolchains\v2.1.0\opt\bin\cmake.exe -E touch C:/nordic/myapps/external_lib/build/mylib/src/mylib_project-stamp/mylib_project-build"
    'make' is not recognized as an internal or external command,
    operable program or batch file.
    [8/173] Generating misc/generated/syscalls.json, misc/generated/struct_tags.json
    ninja: build stopped: subcommand failed.
    FATAL ERROR: command exited with status 1: 'c:\ncs\toolchains\v2.1.0\opt\bin\cmake.EXE' --build 'c:\nordic\myapps\external_lib\build'
    
     *  The terminal process terminated with exit code: 1. 
     *  Terminal will be reused by tasks, press any key to close it. 

    I also tried with bash command with the example in SDK examples like mentioned in  external_lib not compiling out of the box .

    I get the same error. I have C:\Program Files (x86)\Nordic Semiconductor\nrf-command-line-tools\bin\ in my PATH.

    What it could be the reason of the error?

  • The sample unfortunately relies on "make" for creating the static library.

    Your system does not have "make" - which you can get from mingw32 for instance:

    https://sourceforge.net/projects/mingw/files/MinGW/Extension/make/mingw32-make-3.80-3/

    (note: put it in path and rename to "make")

     

    Is your intention to create a library or to include a static library? If it's the latter, please see my post on how to easier include a static library to your project.

     

    Kind regards,

    Håkon

     

Related