编译警告时如何解决不推荐使用的函数声明?

Suv*_*_Eu -1 c embedded compiler-warnings keil

我有 3 个文件依赖项,其中一个我实现了一个函数,您可以在下面看到第一个。它返回一个 32 位的 int 类型。第二个文件实现了一个调用第一个文件的函数的函数。该函数将从主文件(第三个文件)中调用。当我编译(使用 keil IDE)所有文件时,我有以下警告消息:

compiling App_Task_CAN.c...
..\src\Application_Tasks\App_Task_CAN.h(132): warning:  #1295-D: Deprecated      declaration App_Task_CAN_GetError - give arg types
  uint32_t App_Task_CAN_GetError();
Run Code Online (Sandbox Code Playgroud)

这是我包含 stm32l4xx_hal_can.c 的文件中代码的一部分:

 uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
 {
      return hcan->ErrorCode;
 }
Run Code Online (Sandbox Code Playgroud)

其中 hcan 在 stm32l4xx_hal_can.ha 类型中:

   /**
   * @brief  CAN handle Structure definition
   */
 typedef struct
 {
     CAN_TypeDef                 *Instance;  /*!< Register base address          */

     CAN_InitTypeDef             Init;       /*!< CAN required parameters        */

     CanTxMsgTypeDef*            pTxMsg;     /*!< Pointer to transmit structure  */

     CanRxMsgTypeDef*            pRxMsg;     /*!< Pointer to reception structure */

     __IO HAL_CAN_StateTypeDef   State;      /*!< CAN communication state        */

     HAL_LockTypeDef             Lock;       /*!< CAN locking object             */

     __IO uint32_t               ErrorCode;  /*!< CAN Error code                 */ 

}CAN_HandleTypeDef;
Run Code Online (Sandbox Code Playgroud)

此外,我在调用 App_Task_CAN.c 中第一个显示的函数的其他文件中有此实现:

...
CAN_HandleTypeDef HCAN_Struct;//hcan struct type declaration in this file

uint32_t App_Task_CAN_GetError()
{   
    static uint32_t ErrorNumb;

    ErrorNumb=HAL_CAN_GetError(&HCAN_Struct);

    return ErrorNumb; //I want this functions returns an int value.
}
Run Code Online (Sandbox Code Playgroud)

main.c 中的主要代码:

int main(void)
{
    uint32_t    ErrorType=0;

    while (1)
    {
        ErrorType=App_Task_CAN_GetError(); //it takes the resulting value of calling HAL_CAN_GetError(&HCAN_Struct)
    }
}
Run Code Online (Sandbox Code Playgroud)

当我传递参数时,警告消失。但我不知道为什么它要求我提供保存返回值的参数。

为什么它需要一个参数,因为它返回一个 uint32_t?是否可以改进代码以便在 main 调用 App_Task_CAN_GetError() 时从 main 获取更新的 ErrorType?

注意:stm32l4xx_hal_can.c 和.h 不能修改,因为它们是系统库文件。

LPs*_*LPs 5

使用,声明

int foo(void) 
Run Code Online (Sandbox Code Playgroud)

表示返回int带参数的函数。声明

int f() 
Run Code Online (Sandbox Code Playgroud)

意味着一个函数返回int,它接受任意数量的参数

因此,如果您有一个在不带参数的函数,则前者是正确的原型。

  • 另请注意,编译器正确地警告此功能已被弃用,根据 6.11.6 函数声明符“使用带空括号的函数声明符(不是原型格式参数类型声明符)是一项过时的功能。” (2认同)