Search code examples
cstm32weak-linkingcmock

__attribute__((weak) ) function result in undefined reference


I am currently trying to set up CMock for unit testing the STM32 using MinGW and CMake. In my config file, I set the :weak: option so that the generated mock would give me weak functions. One example is:

#if defined (__IAR_SYSTEMS_ICC__)
#pragma weak HAL_TIM_IC_Init
#else
HAL_StatusTypeDef  __attribute__((weak)) HAL_TIM_IC_Init(TIM_HandleTypeDef* htim);
#endif

HAL_StatusTypeDef HAL_TIM_IC_Init(TIM_HandleTypeDef* htim)
{
  ...
}

However, when I compile, it gives me undefined reference to HAL_TIM_IC_Init error.

If I remove the weak attributes, then it would not give me the undefined reference error. But because I need to override some function provided by the HAL layer, I need to make the mocked library as weak, otherwise it will give me multiple definition errors.

So my question is why the weak attribute caused the undefined reference error and how can I work around it?


Solution

  • So apparently, MinGW doesn't support weak attribute. When i move to ubuntu then it is ok.

    Not sure if i am correct, but there might still be an alternative to weak attribute for MinGW, that is to use __declspec(selectany), but it seems like it is only for variable, because when i apply to function then it gives me this error 'selectany' attribute applies only to initialized variables with external linkage