2

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?

harper
  • 13,345
  • 8
  • 56
  • 105
Le Hoang Long
  • 428
  • 3
  • 10
  • 1
    Post the linking command. Check the order of the libraries. Tip: `target_link_libraries(target -Wl,-whole-archive LIBS_HERE -Wl,-no-whole-archive)` or `target_link_libraries(target LIBS_HERE REPEAT_ALL_THE_LIBS_AGAIN)` – KamilCuk Mar 19 '19 at 12:31
  • I have tried what you said but it didnt work . `add_library(mockLibrary STATIC ${LIB_SOURCES})` and then `TARGET_LINK_LIBRARIES(TestPortTimer mockLibrary mockLibrary)` The link libraries sources are found by `file(GLOB LIB_SOURCES LIST_DIRECTORIES true ...)` – Le Hoang Long Mar 20 '19 at 01:30
  • FYI, if i remove the `HAL_StatusTypeDef __attribute__((weak)) HAL_TIM_IC_Init(TIM_HandleTypeDef* htim);` declaration line or simply the `__attribute__((weak))`, then the problem disappear. But i dont want to do it manually, since the files are auto-generated. So what is the problem here? – Le Hoang Long Mar 20 '19 at 01:54

1 Answers1

1

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

Le Hoang Long
  • 428
  • 3
  • 10