Running ztest unit tests when the device boots solution - undefined reference to `__device_dts_ord_186'

I am developing an application for an NRF52840 using Zephyr and trying to run unit tests on startup. My application sources are in my src folder, and my unit ztests are in the folder called tests.

My application builds fine, but now that I am trying to enable an option to build and run the unit tests at bootup, I am having linker issues as below.   

My prj.conf for my application has the the following two lines at the bottom. I have defined CONFIG_RUN_TESTS_AT_BOOT in my Kconfig file. 

CONFIG_ZTEST=y
CONFIG_RUN_TESTS_AT_BOOT=y

My CMakeLists.txt : 

if(CONFIG_RUN_TESTS_AT_BOOT)
  target_sources(app PRIVATE tests/src/main.c)
endif()

Then in my main application source file : 

#ifdef CONFIG_RUN_TESTS_AT_BOOT
    ztest_run_all(NULL, false, -1, -1);
#endif

Is this even the correct way to execute ztests from my application at bootup? This is the linker errors I am getting. There are many more on trying to find __device_dts_ord_186

/home/user/zephyr-sdk-0.17.0/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: modules/chester/subsys/ctr_lte_v2/lib..__chester__subsys__ctr_lte_v2.a(ctr_lte_v2_flow.c.obj): in function `ctr_lte_v2_flow_reset':
/home/user/top_main/subsys/ctr_lte_v2/ctr_lte_v2_flow.c:865: undefined reference to `__device_dts_ord_186'

  •  Unfortunatley I have no idea how to solve this

  • I can't pinpoint the problem based on the information provided. What’s the difference between the working and failing builds? Are you building for the same board in both cases?

  •  The working build just builds my application and doesn't link and try to call the ztests which are in the tests directory. Both builds are for the same board. I try to execute the ztests from my application code as follows :

    #ifdef CONFIG_RUN_TESTS_AT_BOOT
    ztest_run_all(NULL, false, -1, -1);
    #endif

    My project structure is like this 

    firmware/ 

                 ----src/ 

                           --- prj.conf

                           ---CMakeLists.txt

                           --- main.c

                 ---tests/

                        --- auth_tests/

                                           ----src/ 

                                                     ---- auth_test.c

    Here is the CMakeLists.txt for my application : 

    #
    # Copyright (c) 2024 HARDWARIO a.s.
    #
    # SPDX-License-Identifier: LicenseRef-HARDWARIO-5-Clause
    #
    
    cmake_minimum_required(VERSION 3.20.0)
    
    set(ENV{FW_BUNDLE} "com.hardwario.chester.app.belter_poc")
    set(ENV{FW_VERSION} "v1.0.0")
    set(ENV{FW_NAME} "CHESTER-belter_poc")
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    
    project(chester-belter_poc)
    
    add_custom_command(
        COMMAND west gen-codec -d ../codec/cbor-decoder.yaml -e ../codec/cbor-encoder.yaml -o ../src/app_codec.h
        OUTPUT ../src/app_codec.h
        DEPENDS ../codec/cbor-decoder.yaml
    )
    
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/src)
    
    target_sources(app PRIVATE src/app_shell.c)
    target_sources(app PRIVATE src/app_config.c)
    target_sources(app PRIVATE src/app_data.c)
    target_sources(app PRIVATE src/app_codec.h)
    target_sources(app PRIVATE src/app_handler.c)
    target_sources(app PRIVATE src/app_init.c)
    target_sources(app PRIVATE src/app_work.c)
    target_sources(app PRIVATE src/app_cbor.c)
    target_sources(app PRIVATE src/main.c)
    target_sources(app PRIVATE src/app_sensor.c)
    target_sources(app PRIVATE src/app_alarm.c)
    target_sources(app PRIVATE src/app_top_lock.c)
    target_sources(app PRIVATE src/app_lights.c)
    target_sources(app PRIVATE src/app_buzzer.c)
    target_sources(app PRIVATE src/app_send_data.c)
    target_sources(app PRIVATE src/app_lte.c)
    target_sources(app PRIVATE src/app_acc.c)
    target_sources(app PRIVATE src/app_ble.c)
    target_sources(app PRIVATE src/app_ble_auth.c)
    target_sources(app PRIVATE src/app_ble_auth.c)
    target_sources(app PRIVATE src/app_ble_frag.c)
    target_sources(app PRIVATE src/peripheral_manager.c)
    
    target_precompile_headers(app PRIVATE src/feature.h)
    
    # This causes the linker error
    # if(CONFIG_RUN_TESTS_AT_BOOT)
    #   target_sources(app PRIVATE tests/auth_tests/src/auth_test.c)
    # endif()
    
    
    # ### Preserved code "cmake" (begin)
    # ^^^ Preserved code "cmake" (end)

    Here is the prj.conf for my application : 

    #
    # Copyright (c) 2024 HARDWARIO a.s.
    #
    # SPDX-License-Identifier: LicenseRef-HARDWARIO-5-Clause
    #
    
    # Configurations
    # SUBSYSTEM-ACCEL
    CONFIG_CTR_ACCEL=y
    # SUBSYSTEM-BATT
    # @erich CONFIG_CTR_BATT=y
    # SUBSYSTEM-BLUETOOTH
    CONFIG_CTR_BLE=y
    # SUBSYSTEM-BUF
    CONFIG_CTR_BUF=y
    # SUBSYSTEM-BUTTON
    CONFIG_CTR_BUTTON=y
    # SUBSYSTEM-CLOUD
    CONFIG_CTR_CLOUD=y
    CONFIG_SETTINGS_SHELL=y
    CONFIG_SETTINGS_FCB=y
    CONFIG_FCB=y
    CONFIG_IMG_MANAGER=y
    CONFIG_BOOTLOADER_MCUBOOT=y
    CONFIG_PM_PARTITION_SIZE_SETTINGS_STORAGE=0x8000
    CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y
    CONFIG_SPI=y
    CONFIG_DFU_TARGET=y
    CONFIG_DFU_TARGET_STREAM=y
    CONFIG_DFU_TARGET_MCUBOOT=y
    CONFIG_DFU_TARGET_LOG_LEVEL_DBG=y
    CONFIG_STREAM_FLASH_ERASE=y
    CONFIG_STREAM_FLASH=y
    CONFIG_FILE_SYSTEM=y
    CONFIG_FILE_SYSTEM_LITTLEFS=y
    CONFIG_FILE_SYSTEM_MKFS=y
    CONFIG_FILE_SYSTEM_SHELL=y
    CONFIG_SETTINGS_FILE=y
    CONFIG_SETTINGS_FILE_PATH="/lfs1/settings/run"
    CONFIG_PM_PARTITION_REGION_LITTLEFS_EXTERNAL=y
    # SUBSYSTEM-CONFIG
    CONFIG_CTR_CONFIG=y
    # SUBSYSTEM-DEFAULTS
    CONFIG_CTR_DEFAULTS=y
    # SUBSYSTEM-ENTROPY-GENERATOR
    CONFIG_ENTROPY_GENERATOR=y
    CONFIG_TEST_RANDOM_GENERATOR=y
    # SUBSYSTEM-GNSS
    CONFIG_CTR_GNSS=y
    # SUBSYSTEM-INFO
    CONFIG_CTR_INFO=y
    # SUBSYSTEM-LED
    CONFIG_CTR_LED=y
    # SUBSYSTEM-LOG
    CONFIG_CTR_LOG=y
    # SUBSYSTEM-LTE-V2
    CONFIG_CTR_LTE_V2=y
    # SUBSYSTEM-RTC
    CONFIG_CTR_RTC=y
    # SUBSYSTEM-SHELL
    CONFIG_CTR_SHELL=y
    # SUBSYSTEM-THERM
    CONFIG_CTR_THERM=y
    # SUBSYSTEM-TINYCRYPT
    CONFIG_TINYCRYPT=y
    # SUBSYSTEM-TINYCRYPT-SHA256
    CONFIG_TINYCRYPT_SHA256=y
    # SUBSYSTEM-WDOG
    CONFIG_CTR_WDOG=y
    # SUBSYSTEM-ZCBOR
    CONFIG_ZCBOR=y
    CONFIG_ZCBOR_STOP_ON_ERROR=y
    # Extra configurations
    CONFIG_ADC_NRFX_SAADC=n
    CONFIG_ADC_SHELL=n
    CONFIG_ADC_TLA2021_INIT_PRIORITY=60
    CONFIG_CTR_LTE_V2_GNSS=y
    CONFIG_NEWLIB_LIBC_NANO=n
    CONFIG_CTR_GNSS_LOG_LEVEL_DBG=y
    CONFIG_GNSS_M8_LOG_LEVEL_DBG=y
    CONFIG_CTR_GNSS_M8=y
    CONFIG_CTR_GPIO=y
    
    
    CONFIG_BT=y
    CONFIG_BT_PERIPHERAL=y
    #CONFIG_BT_GAP_PERIPHERAL_PREF_PARAMS=n
    CONFIG_BT_BONDABLE=n
    CONFIG_BT_SMP=n
    CONFIG_BT_SETTINGS=y 
    
    CONFIG_BT_DEVICE_NAME="Belter GATT Server"
    
    # GATT dynamic DB support
    CONFIG_BT_GATT_DYNAMIC_DB=y
    
    # Optional: Change MTU size
    CONFIG_BT_L2CAP_TX_MTU=247
    CONFIG_BT_BUF_ACL_RX_SIZE=251
    CONFIG_BT_BUF_ACL_TX_SIZE=251
    
    # Logging (optional but useful)
    CONFIG_LOG=y
    CONFIG_LOG_MODE_IMMEDIATE=y
    
    CONFIG_POSIX_API=y
    CONFIG_POSIX_TIMERS=y
    
    # Enable Nordic Crypto backend
    CONFIG_NRF_SECURITY=y
    
    CONFIG_MBEDTLS_LIBRARY=y
    CONFIG_MBEDTLS=y
    CONFIG_MBEDTLS_SHA256_C=y
    CONFIG_MBEDTLS_PSA_CRYPTO_C=y
    CONFIG_MBEDTLS_HKDF_C=y
    CONFIG_MBEDTLS_BUILTIN=y
    CONFIG_MBEDTLS_CIPHER_MODE_CBC=y
    CONFIG_MBEDTLS_AES_C=y
    CONFIG_MBEDTLS_HEAP_SIZE=8192
    CONFIG_HEAP_MEM_POOL_SIZE=4096
    CONFIG_MBEDTLS_ENABLE_HEAP=y
    
    CONFIG_PSA_WANT_KEY_TYPE_AES=y
    CONFIG_PSA_WANT_AES_KEY_SIZE_128=y
    CONFIG_PSA_WANT_ALG_CMAC=y
    CONFIG_PSA_WANT_ALG_ECB_NO_PADDING=y 
    CONFIG_PSA_WANT_ALG_CBC_NO_PADDING=y
    
    
    
    #CONFIG_ZTEST=y
    CONFIG_RUN_TESTS_AT_BOOT=y
    
    
    
    # ### Preserved code "config" (begin)
    # ^^^ Preserved code "config" (end)
    

    Here is how the auth_test.c looks : 

    #include <zephyr/kernel.h>
    
    #include <zephyr/types.h>
    #include <zephyr/ztest.h>
    
    /* Standard includes */
    #include <stdbool.h>
    #include <stddef.h>
    #include <stdint.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <sys/types.h>
    
    #include "auth_test.h" // Header file of the functions to call in my application code
    
    
    // Test 1
    ZTEST(authentication_test, test_auth) {
        // test logic - calls functions in my application code to test
    }
    
    
    ZTEST_SUITE(authentication_test, NULL, NULL, NULL, NULL, NULL);

     I really don't get why there is a linker error for the 186 /soc/uart@40002000/ctr_lte_link
    in this case when just linking the tests.
  • Is ctr_lte_v2_flow.c included in the normal build? Are you the author of this file?

  •  ctr_lte_v2_flow.c should be included in the build. i am not the author of that file / module. 

Related