首页 > 解决方案 > 为什么 IAR 中的 __attribute__((weak)) 无法编译?

问题描述

我正在 IAR 上研究 STM32F1,我编写了一个弱函数使用

__attribute__((weak))

主程序

#include "tmp.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int testfunc1(int a)
{
  return true;
}

int main(void)
{
  while (1)
  {
  }
}

时间

#include <stdio.h>
#include <stdlib.h>


int testfunc1(int a);

tmp.c

#include "tmp.h"

__attribute__((weak)) int testfunc1(int a)
{    
}

它编译时出现错误:

Error[Pe079]: expected a type specifier
Warning[Pe606]: this pragma must immediately precede a declaration 
Error[Pe260]: explicit type is missing ("int" assumed) 
Error[Pe141]: unnamed prototyped parameters not allowed when body is present 
Error[Pe130]: expected a "{" 
Error while running C/C++ Compiler 

但是,如果我使用 __weak 而不是属性((弱)),它会按预期正常工作。

tmp.c

#include "tmp.h"

__weak int testfunc1(int a)
{
}

.

Warning[Pe940]: missing return statement at end of non-void function "testfunc1"  
Done. 0 error(s), 1 warning(s) 

那么,为什么属性((弱))不起作用?

标签: cstm32iarstm32f1

解决方案


为什么__attribute__((weak))在 IAR 中无法编译?

为什么属性((弱))不起作用?

因为您使用的 IAR 编译器版本不支持它。

我相信大多数“为什么”问题都是不好的问题。对“为什么”某事发生的答案要么太宽泛(需要解释一切),要么太模糊(因为事情就是这样)。在这种情况下,您的编译器只是不支持该特定语法。为了进一步调查“为什么”,IAR Systems 公司决定不为该 IAR 编译器版本实现对特定语法的支持,请询问该公司。


推荐阅读