nRF91集成Azure IoT Hub DPS遇函数未定义引用错误求助
编译nRF91应用时遇到以下链接错误:
C:/Projects/myProject/src/main.cpp:63: undefined reference to
azure_iot_hub_dps_init' c:/ncs/toolchains/b620d30767/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: C:/Projects/myProject/src/main.cpp:70: undefined reference toazure_iot_hub_dps_start'
c:/ncs/toolchains/b620d30767/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: C:/Projects/myProject/src/main.cpp:87: undefined reference toazure_iot_hub_dps_hostname_get' c:/ncs/toolchains/b620d30767/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd.exe: C:/Projects/myProject/src/main.cpp:94: undefined reference toazure_iot_hub_dps_device_id_get'
头文件已被正确找到,编译器未报头文件相关错误,代码已对照官方示例检查过,自认为实现正确。
我的源代码:
int main(void) { int err; struct azure_iot_hub_buf assigned_hostname; struct azure_iot_hub_buf assigned_device_id; /* Ensure that the buffers for device ID and ID scope are properly initialized */ char device_id_buf[32] = {0}; // Adjust size as necessary size_t device_id_len = sizeof(device_id_buf); char id_scope_buf[32] = {0}; // Adjust size as necessary size_t id_scope_len = sizeof(id_scope_buf); /* Initialize DPS configuration */ struct azure_iot_hub_dps_config dps_cfg = { .handler = dps_handler, .id_scope = { .ptr = id_scope_buf, .size = id_scope_len, }, .reg_id = { .ptr = device_id_buf, .size = device_id_len, }, }; /* Initialize DPS */ err = azure_iot_hub_dps_init(&dps_cfg); if (err < 0) { LOG_ERR("Failed to initialize DPS: %d", err); return 0; } /* Start DPS */ err = azure_iot_hub_dps_start(); if (err == 0) { LOG_INF("The DPS process has started"); /* Wait for the registration process to complete */ err = k_sem_take(&dps_done_sem, K_SECONDS(SOME_TIMEOUT)); if (err < 0) { LOG_ERR("DPS registration timed out: %d", err); return 0; } } else if (err == -EALREADY) { LOG_INF("Already assigned to an IoT hub, skipping DPS"); } else { LOG_ERR("Failed to start DPS: %d", err); return 0; } /* Retrieve assigned hostname and device ID */ err = azure_iot_hub_dps_hostname_get(&assigned_hostname); if (err < 0) { LOG_ERR("Failed to get assigned hostname: %d", err); return 0; } LOG_INF("Assigned hostname: %.*s", assigned_hostname.size, assigned_hostname.ptr); err = azure_iot_hub_dps_device_id_get(&assigned_device_id); if (err < 0) { LOG_ERR("Failed to get assigned device ID: %d", err); return 0; } LOG_INF("Assigned device ID: %.*s", assigned_device_id.size, assigned_device_id.ptr); /* Initialize Azure IoT Hub configuration */ struct azure_iot_hub_config cfg = { .hostname = { .ptr = assigned_hostname.ptr, .size = assigned_hostname.size, }, .device_id = { .ptr = assigned_device_id.ptr, .size = assigned_device_id.size, }, .use_dps = true, }; /* Connect to Azure IoT Hub */ err = azure_iot_hub_connect(&cfg); if (err < 0) { LOG_ERR("Failed to connect to Azure IoT Hub: %d", err); return 0; } LOG_INF("Successfully connected to Azure IoT Hub"); return 0; }
请问我遗漏了什么?已经研究很久还是没找到问题。
出现这种未定义引用错误,核心原因是链接器找不到这些DPS函数的实现代码——头文件能被找到只说明编译阶段没问题,但链接阶段需要把对应的库文件/编译单元包含进来。在nRF Connect SDK(NCS)中,Azure IoT Hub的DPS功能需要通过Kconfig选项显式启用,否则相关代码不会被编译进最终固件。
你需要做以下操作:
打开项目的
prj.conf文件,添加以下配置项:CONFIG_AZURE_IOT_HUB=y CONFIG_AZURE_IOT_HUB_DPS=yCONFIG_AZURE_IOT_HUB=y:启用基础的Azure IoT Hub库CONFIG_AZURE_IOT_HUB_DPS=y:显式启用DPS相关功能模块
如果你的项目使用了nRF91常用的TLS后端(如Certicom安全库),还需要确保相关安全配置已启用:
CONFIG_CERTIFICATE_MANAGER=y CONFIG_MBEDTLS=y重新执行编译命令(比如
west build),链接器就能找到azure_iot_hub_dps_init等函数的实现了。
另外,在NCS的Zephyr框架下,只要Kconfig选项开启,CMake会自动处理库的链接,所以重点是确认Kconfig配置是否正确。
内容的提问来源于stack exchange,提问作者Mart

