邢台市网站建设_网站建设公司_原型设计_seo优化
2025/12/27 15:28:35 网站建设 项目流程

[OpenHarmony] 记一次 LiteOS-M 移植编译报错解决:PRId64 未定义与类型转换警告

在进行 OpenHarmony(LiteOS-M 内核)涉及 ARM Cortex-M4 芯片(如 HC32F467)的移植开发过程中,我们经常会遇到因为工具链差异或编译器版本过高导致的编译错误。本文记录了两个典型的报错案例及其“零代码侵入”的修复方案。

环境背景

  • 系统:OpenHarmony (LiteOS-M)
  • 目标芯片:HC32F467 (Cortex-M4)
  • 编译器:arm-none-eabi-gcc
  • 涉及模块device_auth (设备认证子系统)

问题一:PRId64 未定义报错

1. 报错现象

在编译 base/security/device_auth 模块时,出现如下错误:

[OHOS ERROR] ../../../base/security/device_auth/common_lib/impl/src/json_utils.c: In function 'AddInt64StringToJson':
[OHOS ERROR] ../../../base/security/device_auth/common_lib/impl/src/json_utils.c:498:46: error: expected ')' before 'PRId64'
[OHOS ERROR]   498 |     if (sprintf_s(buffer, sizeof(buffer), "%" PRId64, value) <= 0) {
详细报错原文:
[OHOS INFO] [598/864] ACTION //drivers/hdf_core/framework/tools/hc-gen:build_hc_gen(//build/lite/toolchain:arm-none-eabi-gcc)
[OHOS ERROR] [585/864] gcc cross compiler obj/base/security/device_auth/common_lib/impl/src/libdeviceauth_hal_liteos.json_utils.o
[OHOS ERROR] FAILED: obj/base/security/device_auth/common_lib/impl/src/libdeviceauth_hal_liteos.json_utils.o
[OHOS ERROR] ccache arm-none-eabi-gcc -D_XOPEN_SOURCE=700 -DOHOS_DEBUG -D__LITEOS__ -D__LITEOS_M__ -DSECUREC_IN_KERNEL=0 -I../../../base/security/device_auth/common_lib/interfaces -I../../../base/security/device_auth/deps_adapter/key_management_adapter/interfaces -I../../../base/security/device_auth/deps_adapter/key_management_adapter/impl/inc -I../../../base/security/device_auth/deps_adapter/os_adapter/interfaces -I../../../base/security/device_auth/deps_adapter/os_adapter/interfaces/liteos -I../../../base/security/device_auth/interfaces/inner_api -I../../../third_party/cJSON -I../../../commonlibrary/utils_lite/include -I../../../base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite -I../../../commonlibrary/utils_lite/memory/include -I../../../base/startup/init/interfaces/innerkits/include/syspara -I../../../kernel/liteos_m/arch/arm/common -I../../../kernel/liteos_m/arch/arm/cortex-m4/gcc -I../../../kernel/liteos_m/arch/arm/include -I../../../kernel/liteos_m/arch/include -I../../../kernel/liteos_m/kernel/include -I../../../third_party/cmsis/CMSIS/Core/Include -I../../../third_party/cmsis/CMSIS/RTOS2/Include -I../../../third_party/cmsis/CMSIS/DSP/Include -I../../../third_party/cmsis/CMSIS/DSP/PrivateInclude -I../../../kernel/liteos_m/kal/cmsis -I../../../kernel/liteos_m/kal/libc/newlib/porting/include -I../../../third_party/bounds_checking_function/include -I../../../kernel/liteos_m/kal/posix/include -I../../../kernel/liteos_m/components/fs/vfs -I../../../third_party/littlefs -I../../../third_party/littlefs/bd -I../../../kernel/liteos_m/components/fs/littlefs -I../../../kernel/liteos_m/components/power -I../../../kernel/liteos_m/components/shell/include -I../../../kernel/liteos_m/components/signal -I../../../kernel/liteos_m/utils -I../../../device/board/csg/hc32f467_board/liteos_m/board -I../../../device/board/csg/hc32f467_board/liteos_m/board/include -I../../../device/soc/hdsc/hc32f467/Drivers/hc32_ll_driver/inc -I../../../device/soc/hdsc/hc32f467/Drivers/cmsis/Device/HDSC/hc32f4xx/Include -I../../../drivers/hdf_core/framework/include/utils -I../../../drivers/hdf_core/adapter/khdf/liteos_m/osal/include -I../../../drivers/hdf_core/framework/include/platform -I../../../drivers/hdf_core/framework/include/osal -Igen/device/board/csg/hc32f467_board/liteos_m/hdf_config -I../../../device/board/csg/hc32f467_board/liteos_m -I../../../drivers/hdf_core/framework/core/common/include/host -I../../../drivers/hdf_core/adapter/uhdf2/include/devhost -I../../../drivers/hdf_core/framework/support/platform/include/gpio -I../../../drivers/hdf_core/framework/support/platform/include/i2c -I../../../drivers/hdf_core/framework/support/platform/include/spi -I../../../drivers/hdf_core/framework/support/platform/include/adc -I../../../drivers/hdf_core/framework/support/platform/include/dac -I../../../drivers/hdf_core/framework/support/platform/include/rtc -I../../../drivers/hdf_core/framework/support/platform/include/pwm -I../../../drivers/hdf_core/framework/support/platform/include/watchdog -I../../../drivers/hdf_core/framework/support/platform/include/uart -I../../../drivers/hdf_core/framework/support/platform/include/fwk -I../../../drivers/hdf_core/framework/utils/include -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/rtc -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/i2c -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/pwm -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/gpio -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/uart -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/spi -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/lfs -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/adc -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/dma -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/watchdog -I../../../drivers/hdf_core/framework/support/platform/src/rtc -I../../../drivers/hdf_core/framework/core/adapter/vnode/include -I../../../drivers/hdf_core/interfaces/inner_api/utils -I../../../drivers/hdf_core/interfaces/inner_api/osal/shared -I../../../drivers/hdf_core/interfaces/inner_api/host/shared -I../../../drivers/hdf_core/interfaces/inner_api/core -I../../../device/board/csg/hc32f467_board/hdf_test -I../../../drivers/hdf_core/framework/include/core -I../../../drivers/hdf_core/adapter/uhdf2/include/host -I../../../base/security/huks/interfaces/inner_api/huks_standard/main/include -I../../../base/startup/init/interfaces/innerkits/include -I../../../base/startup/init/interfaces/innerkits/include/token -I../../../base/startup/init/services/include -I../../../base/startup/init/services/include/param -I../../../base/startup/init/services/param/include -DHILOG_ENABLE -DAUTH_STORAGE_PATH=\"\" -Os -mcpu=cortex-m4 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Wall -fdata-sections -fno-builtin -fno-strict-aliasing -fsigned-char -std=c99 -O2 -Os -DHC32F467 -DUSE_DDL_DRIVER -D__FPU_PRESENT -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -g -mcpu=cortex-m4 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Wall -fdata-sections -fno-builtin -fno-strict-aliasing -fsigned-char -std=c99 -O2 -Os -DHC32F467 -DUSE_DDL_DRIVER -D__FPU_PRESENT -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -g -mcpu=cortex-m4 -fno-common -fno-builtin -fno-strict-aliasing -Wall -fsigned-char -fstack-protector-all -imacros /media/PowerHarmony_PHM_SDK_V1.0.1.0/out/hc32f467_board/hc32f467_product_demo/config.h -std=c99 -c ../../../base/security/device_auth/common_lib/impl/src/json_utils.c -o obj/base/security/device_auth/common_lib/impl/src/libdeviceauth_hal_liteos.json_utils.o
[OHOS ERROR] ../../../base/security/device_auth/common_lib/impl/src/json_utils.c: In function 'AddInt64StringToJson':
[OHOS ERROR] ../../../base/security/device_auth/common_lib/impl/src/json_utils.c:498:46: error: expected ')' before 'PRId64'
[OHOS ERROR]   498 |     if (sprintf_s(buffer, sizeof(buffer), "%" PRId64, value) <= 0) {
[OHOS ERROR]       |                  ~                           ^~~~~~~
[OHOS ERROR]       |                                              )
[OHOS ERROR] ../../../base/security/device_auth/common_lib/impl/src/json_utils.c:24:1: note: 'PRId64' is defined in header '<inttypes.h>'; did you forget to '#include <inttypes.h>'?
[OHOS ERROR]    23 | #include "string_util.h"
[OHOS ERROR]   +++ |+#include <inttypes.h>
[OHOS ERROR]    24 |
[OHOS ERROR] Traceback (most recent call last):
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/services/ninja.py", line 49, in _execute_ninja_cmd
[OHOS ERROR]     SystemUtil.exec_command(
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/util/system_util.py", line 63, in exec_command
[OHOS ERROR]     raise OHOSException(
[OHOS ERROR] exceptions.ohos_exception.OHOSException: Please check build log in /media/PowerHarmony_PHM_SDK_V1.0.1.0/out/hc32f467_board/hc32f467_product_demo/build.log
[OHOS ERROR]
[OHOS ERROR] During handling of the above exception, another exception occurred:
[OHOS ERROR]
[OHOS ERROR] Traceback (most recent call last):
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/containers/status.py", line 47, in wrapper
[OHOS ERROR]     return func(*args, **kwargs)
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/ohos_build_module.py", line 67, in run
[OHOS ERROR]     raise exception
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/ohos_build_module.py", line 65, in run
[OHOS ERROR]     super().run()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/interface/build_module_interface.py", line 72, in run
[OHOS ERROR]     raise exception
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/interface/build_module_interface.py", line 70, in run
[OHOS ERROR]     self._target_compilation()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/ohos_build_module.py", line 103, in _target_compilation
[OHOS ERROR]     self.target_compiler.run()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/services/ninja.py", line 38, in run
[OHOS ERROR]     self._execute_ninja_cmd()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/services/ninja.py", line 52, in _execute_ninja_cmd
[OHOS ERROR]     raise OHOSException('ninja phase failed', '4000')
[OHOS ERROR] exceptions.ohos_exception.OHOSException: ninja phase failed
[OHOS ERROR]
[OHOS ERROR] Code:      4000
[OHOS ERROR]
[OHOS ERROR] Reason:    ninja phase failed
[OHOS ERROR]
[OHOS ERROR] Solution:  Please check the compile log at out/{compiling product}/build.log, If you could analyze build logs.
[OHOS ERROR]            Or you can try the following steps to solve this problem:
[OHOS ERROR]              1. cd to OHOS root path
[OHOS ERROR]              2. run 'hb clean --all' or 'rm -rf out build/resources/args/*.json'.
[OHOS ERROR]              3. repo sync
[OHOS ERROR]              4. repo forall -c 'git lfs pull'
[OHOS ERROR]              5. bash build/prebuilts_download.sh
[OHOS ERROR]              6. rebuild your product or component
[OHOS ERROR]
[OHOS ERROR]            If you still cannot solve this problem, you could post this problem on:
[OHOS ERROR]              https://gitee.com/openharmony/build/issues
[OHOS ERROR]

2. 原因分析

  • 交叉编译环境差异:虽然宿主机(Linux)的 /usr/include 下有 inttypes.h,但交叉编译器(arm-none-eabi-gcc)使用的是自己的 Sysroot(通常是 Newlib)。
  • 标准库裁剪:嵌入式环境下的 Newlib C 库对 C99 标准的支持可能被裁剪,或者需要特定的宏开关(如 __STDC_FORMAT_MACROS)才能开启 PRId64 等宏定义。
  • 代码依赖:OpenHarmony 的底层安全组件依赖了 inttypes.h 中的格式化宏。

3. 解决方案(推荐)

为了避免修改 OpenHarmony 的核心源代码(保持代码纯净),建议在板级配置文件中通过编译参数注入宏定义。

找到当前开发板的全局配置文件,通常位于:
device/board/{厂商}/{板子型号}/liteos_m/config.gni

board_cflags 中添加 -DPRId64=\"lld\"

# device/board/csg/hc32f467_board/liteos_m/config.gniboard_cflags = ["-mcpu=cortex-m4","-mthumb",# ... 其他原有参数 ...# 【新增】强制定义 PRId64 为 "lld" (long long decimal)"-DPRId64=\"lld\"",
]

问题二:enum constant in boolean context 警告被视为错误

1. 报错现象

修复完上一个问题后,继续编译遇到了新的报错:

[OHOS ERROR] ../../../base/security/device_auth/services/group_manager/src/group_operation/group_operation_common.c: In function 'IsLocalDevice':
[OHOS ERROR] ../../../base/security/device_auth/services/group_manager/src/group_operation/group_operation_common.c:156:9: error: enum constant in boolean context [-Werror=int-in-bool-context]
[OHOS ERROR]   156 |         return HC_ERR_DB;
详细报错原文
[OHOS INFO] [661/864] gcc cross compiler obj/base/security/device_auth/services/data_manager/src/libdeviceauth.data_manager.o
[OHOS ERROR] [648/864] gcc cross compiler obj/base/security/device_auth/services/group_manager/src/group_operation/libdeviceauth.group_operation_common.o
[OHOS ERROR] FAILED: obj/base/security/device_auth/services/group_manager/src/group_operation/libdeviceauth.group_operation_common.o
[OHOS ERROR] ccache arm-none-eabi-gcc -DHILOG_ENABLE -DP2P_PAKE_DL_PRIME_LEN_384 -DENABLE_P2P_BIND_ISO -DENABLE_P2P_AUTH_ISO -D_XOPEN_SOURCE=700 -DOHOS_DEBUG -D__LITEOS__ -D__LITEOS_M__ -DSECUREC_IN_KERNEL=0 -DHIVIEW_FILE_DIR=\"\" -I../../../base/security/device_auth/interfaces/inner_api -I../../../base/security/device_auth/frameworks/inc -I../../../base/security/device_auth/services/cred_manager/inc -I../../../base/security/device_auth/services/cred_manager/inc/account_related -I../../../base/security/device_auth/services/data_manager/inc -I../../../base/security/device_auth/services/privacy_enhancement/inc -I../../../base/security/device_auth/services/group_auth/inc -I../../../base/security/device_auth/services/group_auth/inc/account_unrelated_group_auth -I../../../base/security/device_auth/services/group_auth/inc/account_related_group_auth -I../../../base/security/device_auth/services/group_manager/inc -I../../../base/security/device_auth/services/group_manager/inc/broadcast_manager -I../../../base/security/device_auth/services/group_manager/inc/callback_manager -I../../../base/security/device_auth/services/group_manager/inc/channel_manager -I../../../base/security/device_auth/services/group_manager/inc/channel_manager/soft_bus_channel -I../../../base/security/device_auth/services/frameworks/inc -I../../../base/security/device_auth/services/frameworks/inc/module -I../../../base/security/device_auth/services/frameworks/inc/session -I../../../base/security/device_auth/services/frameworks/inc/os_account_adapter -I../../../base/security/device_auth/services/frameworks/inc/plugin_adapter -I../../../base/security/device_auth/services/frameworks/inc/task_manager -I../../../base/security/device_auth/services/authenticators/inc/account_related -I../../../base/security/device_auth/services/authenticators/inc/account_unrelated -I../../../base/security/device_auth/services/authenticators/inc/account_unrelated/iso_task -I../../../base/security/device_auth/services/authenticators/inc/account_unrelated/iso_task/iso_protocol_task -I../../../base/security/device_auth/services/authenticators/inc/account_unrelated/iso_task/lite_exchange_task -I../../../base/security/device_auth/services/authenticators/inc/account_unrelated/pake_task -I../../../base/security/device_auth/services/authenticators/inc/account_unrelated/pake_task/pake_v1_task -I../../../base/security/device_auth/services/authenticators/inc/account_unrelated/pake_task/pake_v2_task -I../../../base/security/device_auth/services/protocol/inc -I../../../base/security/device_auth/services/protocol/inc/pake_protocol -I../../../base/security/device_auth/services/protocol/inc/pake_protocol/pake_v1_protocol -I../../../base/security/device_auth/services/protocol/inc/pake_protocol/pake_v2_protocol -I../../../base/security/device_auth/services/protocol/inc/pake_protocol/pake_protocol_dl_common -I../../../base/security/device_auth/services/protocol/inc/pake_protocol/pake_protocol_ec_common -I../../../base/security/device_auth/services/protocol/inc/iso_protocol -I../../../base/security/device_auth/services/session_manager/inc -I../../../base/security/device_auth/services/session_manager/inc/session -I../../../base/security/device_auth/services/session_manager/inc/session/v1 -I../../../base/security/device_auth/services/session_manager/inc/session/v1/compatible_bind_sub_session -I../../../base/security/device_auth/services/session_manager/inc/session/v1/compatible_auth_sub_session -I../../../base/security/device_auth/services/session_manager/inc/session/v2 -I../../../base/security/device_auth/services/session_manager/inc/session/v2/auth_sub_session -I../../../base/security/device_auth/services/session_manager/inc/session/v2/auth_sub_session/protocol_lib -I../../../base/security/device_auth/services/session_manager/inc/session/v2/expand_sub_session -I../../../base/security/device_auth/services/session_manager/inc/session/v2/expand_sub_session/expand_process_lib -I../../../base/security/device_auth/services/creds_manager/inc -I../../../base/security/device_auth/services/creds_manager/inc/account_related -I../../../base/security/device_auth/services/mk_agree/inc -I../../../base/security/device_auth/common_lib/interfaces -I../../../base/security/device_auth/deps_adapter/key_management_adapter/interfaces -I../../../base/security/device_auth/deps_adapter/key_management_adapter/impl/inc -I../../../base/security/device_auth/deps_adapter/os_adapter/interfaces -I../../../base/security/device_auth/deps_adapter/os_adapter/interfaces/liteos -I../../../third_party/cJSON -I../../../commonlibrary/utils_lite/include -I../../../third_party/bounds_checking_function/include -I../../../foundation/communication/dsoftbus/interfaces/kits/common -I../../../foundation/communication/dsoftbus/interfaces/kits/transport -I../../../foundation/communication/dsoftbus/interfaces/inner_kits/transport -I../../../base/security/device_auth/services/frameworks/inc/hiview_adapter -I../../../base/hiviewdfx/hilog_lite/interfaces/native/kits/hilog_lite -I../../../kernel/liteos_m/arch/arm/common -I../../../kernel/liteos_m/arch/arm/cortex-m4/gcc -I../../../kernel/liteos_m/arch/arm/include -I../../../kernel/liteos_m/arch/include -I../../../kernel/liteos_m/kernel/include -I../../../third_party/cmsis/CMSIS/Core/Include -I../../../third_party/cmsis/CMSIS/RTOS2/Include -I../../../third_party/cmsis/CMSIS/DSP/Include -I../../../third_party/cmsis/CMSIS/DSP/PrivateInclude -I../../../kernel/liteos_m/kal/cmsis -I../../../kernel/liteos_m/kal/libc/newlib/porting/include -I../../../kernel/liteos_m/kal/posix/include -I../../../kernel/liteos_m/components/fs/vfs -I../../../third_party/littlefs -I../../../third_party/littlefs/bd -I../../../kernel/liteos_m/components/fs/littlefs -I../../../kernel/liteos_m/components/power -I../../../kernel/liteos_m/components/shell/include -I../../../kernel/liteos_m/components/signal -I../../../kernel/liteos_m/utils -I../../../device/board/csg/hc32f467_board/liteos_m/board -I../../../device/board/csg/hc32f467_board/liteos_m/board/include -I../../../device/soc/hdsc/hc32f467/Drivers/hc32_ll_driver/inc -I../../../device/soc/hdsc/hc32f467/Drivers/cmsis/Device/HDSC/hc32f4xx/Include -I../../../drivers/hdf_core/framework/include/utils -I../../../drivers/hdf_core/adapter/khdf/liteos_m/osal/include -I../../../drivers/hdf_core/framework/include/platform -I../../../drivers/hdf_core/framework/include/osal -Igen/device/board/csg/hc32f467_board/liteos_m/hdf_config -I../../../device/board/csg/hc32f467_board/liteos_m -I../../../drivers/hdf_core/framework/core/common/include/host -I../../../drivers/hdf_core/adapter/uhdf2/include/devhost -I../../../drivers/hdf_core/framework/support/platform/include/gpio -I../../../drivers/hdf_core/framework/support/platform/include/i2c -I../../../drivers/hdf_core/framework/support/platform/include/spi -I../../../drivers/hdf_core/framework/support/platform/include/adc -I../../../drivers/hdf_core/framework/support/platform/include/dac -I../../../drivers/hdf_core/framework/support/platform/include/rtc -I../../../drivers/hdf_core/framework/support/platform/include/pwm -I../../../drivers/hdf_core/framework/support/platform/include/watchdog -I../../../drivers/hdf_core/framework/support/platform/include/uart -I../../../drivers/hdf_core/framework/support/platform/include/fwk -I../../../drivers/hdf_core/framework/utils/include -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/rtc -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/i2c -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/pwm -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/gpio -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/uart -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/spi -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/lfs -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/adc -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/dma -I../../../device/board/csg/hc32f467_board/liteos_m/hdf_drivers/watchdog -I../../../drivers/hdf_core/framework/support/platform/src/rtc -I../../../drivers/hdf_core/framework/core/adapter/vnode/include -I../../../drivers/hdf_core/interfaces/inner_api/utils -I../../../drivers/hdf_core/interfaces/inner_api/osal/shared -I../../../drivers/hdf_core/interfaces/inner_api/host/shared -I../../../drivers/hdf_core/interfaces/inner_api/core -I../../../device/board/csg/hc32f467_board/hdf_test -I../../../drivers/hdf_core/framework/include/core -I../../../drivers/hdf_core/adapter/uhdf2/include/host -I../../../base/hiviewdfx/hiview_lite -I../../../foundation/systemabilitymgr/samgr_lite/interfaces/kits/samgr -I../../../base/hiviewdfx/hievent_lite/interfaces/native/innerkits -Werror -DHICHAIN_THREAD_STACK_SIZE=8192 -Os -mcpu=cortex-m4 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Wall -fdata-sections -fno-builtin -fno-strict-aliasing -fsigned-char -std=c99 -O2 -Os -DHC32F467 -DUSE_DDL_DRIVER -D__FPU_PRESENT -DPRId64=\"lld\" -g -mcpu=cortex-m4 -mthumb -mthumb-interwork -mfpu=fpv4-sp-d16 -mfloat-abi=hard -Wall -fdata-sections -fno-builtin -fno-strict-aliasing -fsigned-char -std=c99 -O2 -Os -DHC32F467 -DUSE_DDL_DRIVER -D__FPU_PRESENT -DPRId64=\"lld\" -g -mcpu=cortex-m4 -fno-common -fno-builtin -fno-strict-aliasing -Wall -fsigned-char -fstack-protector-all -imacros /media/PowerHarmony_PHM_SDK_V1.0.1.0/out/hc32f467_board/hc32f467_product_demo/config.h -std=c99 -c ../../../base/security/device_auth/services/group_manager/src/group_operation/group_operation_common.c -o obj/base/security/device_auth/services/group_manager/src/group_operation/libdeviceauth.group_operation_common.o
[OHOS ERROR] ../../../base/security/device_auth/services/group_manager/src/group_operation/group_operation_common.c: In function 'IsLocalDevice':
[OHOS ERROR] ../../../base/security/device_auth/services/group_manager/src/group_operation/group_operation_common.c:156:9: error: enum constant in boolean context [-Werror=int-in-bool-context]
[OHOS ERROR]   156 |         return HC_ERR_DB;
[OHOS ERROR]       |         ^~~~~~
[OHOS ERROR] cc1: all warnings being treated as errors
[OHOS ERROR] Traceback (most recent call last):
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/services/ninja.py", line 49, in _execute_ninja_cmd
[OHOS ERROR]     SystemUtil.exec_command(
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/util/system_util.py", line 63, in exec_command
[OHOS ERROR]     raise OHOSException(
[OHOS ERROR] exceptions.ohos_exception.OHOSException: Please check build log in /media/PowerHarmony_PHM_SDK_V1.0.1.0/out/hc32f467_board/hc32f467_product_demo/build.log
[OHOS ERROR]
[OHOS ERROR] During handling of the above exception, another exception occurred:
[OHOS ERROR]
[OHOS ERROR] Traceback (most recent call last):
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/containers/status.py", line 47, in wrapper
[OHOS ERROR]     return func(*args, **kwargs)
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/ohos_build_module.py", line 67, in run
[OHOS ERROR]     raise exception
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/ohos_build_module.py", line 65, in run
[OHOS ERROR]     super().run()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/interface/build_module_interface.py", line 72, in run
[OHOS ERROR]     raise exception
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/interface/build_module_interface.py", line 70, in run
[OHOS ERROR]     self._target_compilation()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/modules/ohos_build_module.py", line 103, in _target_compilation
[OHOS ERROR]     self.target_compiler.run()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/services/ninja.py", line 38, in run
[OHOS ERROR]     self._execute_ninja_cmd()
[OHOS ERROR]   File "/media/PowerHarmony_PHM_SDK_V1.0.1.0/build/hb/services/ninja.py", line 52, in _execute_ninja_cmd
[OHOS ERROR]     raise OHOSException('ninja phase failed', '4000')
[OHOS ERROR] exceptions.ohos_exception.OHOSException: ninja phase failed
[OHOS ERROR]
[OHOS ERROR] Code:      4000
[OHOS ERROR]
[OHOS ERROR] Reason:    ninja phase failed
[OHOS ERROR]
[OHOS ERROR] Solution:  Please check the compile log at out/{compiling product}/build.log, If you could analyze build logs.
[OHOS ERROR]            Or you can try the following steps to solve this problem:
[OHOS ERROR]              1. cd to OHOS root path
[OHOS ERROR]              2. run 'hb clean --all' or 'rm -rf out build/resources/args/*.json'.
[OHOS ERROR]              3. repo sync
[OHOS ERROR]              4. repo forall -c 'git lfs pull'
[OHOS ERROR]              5. bash build/prebuilts_download.sh
[OHOS ERROR]              6. rebuild your product or component
[OHOS ERROR]
[OHOS ERROR]            If you still cannot solve this problem, you could post this problem on:
[OHOS ERROR]              https://gitee.com/openharmony/build/issues
[OHOS ERROR]

2. 原因分析

  • 类型不匹配IsLocalDevice 函数预期返回 bool 类型,但代码逻辑中返回了 HC_ERR_DB(这是一个 int 类型的枚举错误码)。
  • 编译器检查变严:较新版本的 GCC 增加了 -Wint-in-bool-context 检查,认为将枚举值直接当作布尔值返回可能存在逻辑风险。
  • Werror 策略:OpenHarmony 编译选项默认开启了 -Werror,将所有警告视为错误,导致编译中断。

3. 解决方案(推荐)

同样,为了不修改系统源码逻辑,我们可以通过关闭该特定警告来通过编译。

继续修改同一个板级配置文件 config.gni,在 board_cflags 中添加 -Wno-int-in-bool-context

# device/board/csg/hc32f467_board/liteos_m/config.gniboard_cflags = ["-mcpu=cortex-m4","-mthumb",# 【新增】解决 PRId64 报错"-DPRId64=\"lld\"",# 【新增】忽略 "枚举在布尔上下文中使用" 的警告"-Wno-int-in-bool-context",
]

总结与最佳实践

在进行 OpenHarmony 设备开发时,遇到编译报错时的排查思路:

  1. 区分环境:明确是宿主机缺文件还是交叉编译工具链缺文件(find /usr/include 通常是误导)。
  2. 不动源码:尽量避免直接修改 base/foundation/ 目录下的 .c/.h 源码。这些代码是系统核心,修改后不利于后续 OTA 或代码同步。
  3. 善用 GN 配置
    • 如果问题只影响单个模块,修改该模块的 BUILD.gn
    • 如果问题是由于板级工具链(Compiler/Libc)特性导致的(如本文案例),最好修改 板级全局配置 (config.gni),这样能一劳永逸地解决该板子下所有模块的类似问题。

通过上述配置修改后,重新执行 hb build,编译顺利通过。
image

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询