【问题标题】:how to solve a deprecate declaration of a function when compiling warning?编译警告时如何解决函数的过时声明?
【发布时间】:2017-03-17 08:50:19
【问题描述】:

我有 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();

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

 uint32_t HAL_CAN_GetError(CAN_HandleTypeDef *hcan)
 {
      return hcan->ErrorCode;
 }

其中 hcan 在 stm32l4xx_hal_can.h 中是一种类型:

   /**
   * @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;

此外,我在其他文件中调用了 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.
}

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)
    }
}

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

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

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

【问题讨论】:

标签: c embedded compiler-warnings keil


【解决方案1】:

使用,声明

int foo(void) 

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

int f() 

表示返回int 的函数,它接受任意数量的参数

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

【讨论】:

  • 另请注意,根据 6.11.6 函数声明符“使用带空括号的函数声明符(不是原型格式参数类型声明符)是正确的,编译器警告此功能被弃用是正确的过时的功能。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-26
  • 2011-01-16
  • 1970-01-01
  • 1970-01-01
  • 2020-02-16
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多