0

I am working on STM32F1 on IAR, I write a weak function using

__attribute__((weak))

main.c

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

int testfunc1(int a)
{
  return true;
}

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

tmp.h

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


int testfunc1(int a);

tmp.c

#include "tmp.h"

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

It compiles with errors:

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 

However, if I use __weak instead of attribute((weak)), it works normally as expected.

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) 

So, why is attribute((weak)) not working?

Olly
  • 23
  • 6
  • Your question is missing [mre]: Please provide code that fails to compile, your compilation command, and version number of your compiler. – user694733 Nov 06 '20 at 10:12
  • Please share a code snippet where you use this attribute. – Rachid K. Nov 06 '20 at 10:27
  • If you generate the preprocessed file, you may see that __weak may be define to an "empty" depending on the target you are compiling for... – Rachid K. Nov 06 '20 at 10:56
  • You still haven't provided your compilation command or compiler version. This question cannot be answered without those. For example, `__attirubute__` is not supported on IAR 5.40, but is supported onm 8.50. It also requires extended language mode during compilation. – user694733 Nov 06 '20 at 11:06
  • 1
    Based on the information in the question my answer is that your compiler is too old. As far a I can see, `__attribute__((weak))` is implemented in iccarm 7.70 and later. – Johan Nov 06 '20 at 15:29

2 Answers2

1

why __attribute__((weak)) in IAR can't compile?

why is attribute((weak)) not working?

Because it's not supported by the version of IAR compiler you are using.

I believe most "why" questions are bad question. An answer to "why" something happens is either too broad (requires to explain everything) or too vague (because this is how it is). In this case your compiler just doesn't support that specific syntax. To further investigate "why" exactly the IAR Systems company decided not to implement support for that particular syntax for that IAR compiler version, ask that company.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111
1

IAR compiler has its own extensions to archive it:

  1. #pragma enter image description here
  2. __weak enter image description here

I strongly suggest to put some effort and read the compiler documentation before posting questions here.

0___________
  • 60,014
  • 4
  • 34
  • 74