简体   繁体   English

__attribute __((weak))函数导致未定义的引用

[英]__attribute__((weak) ) function result in undefined reference

I am currently trying to set up CMock for unit testing the STM32 using MinGW and CMake. 我目前正在尝试设置CMock,以使用MinGW和CMake对STM32进行单元测试。 In my config file, I set the :weak: option so that the generated mock would give me weak functions. 在我的配置文件中,设置:weak:选项,以便生成的模拟将给我弱函数。 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. 但是,当我编译时,它为我提供了对HAL_TIM_IC_Init错误的未定义引用。

If I remove the weak attributes, then it would not give me the undefined reference error. 如果删除weak属性,则不会给我未定义的参考错误。 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. 但是因为我需要重写HAL层提供的某些功能,所以我需要使模拟库变弱,否则它将给我带来多个定义错误。

So my question is why the weak attribute caused the undefined reference error and how can I work around it? 所以我的问题是,为什么weak属性会导致未定义的引用错误,我该如何解决呢?

So apparently, MinGW doesn't support weak attribute. 因此很明显,MinGW不支持弱属性。 When i move to ubuntu then it is ok. 当我移至ubuntu时就可以了。

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 不确定我是否正确,但是对于MinGW来说,仍然可能有弱属性的替代方法,那就是使用__declspec(selectany),但似乎只适用于变量,因为当我将其应用于函数时,它会给我这个错误的“ selectany”属性仅适用于具有外部链接的初始化变量

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM