【问题标题】:Undefined reference to static variable with -O0 optimization [stm32 template Pins]使用 -O0 优化对静态变量的未定义引用 [stm32 模板引脚]
【发布时间】:2016-06-16 15:35:11
【问题描述】:

我在 Eclipse 中为 STM32F100x 编写程序。为方便起见,我使用this template class 进行引脚控制。

我有这样的代码:

...
Pin<'C', 6>     temp_ok;
Pin<'C', 7>     temp_fail;
...
int main()
{
   ...
   if(temperature > 30)
   {
       temp_ok.Off();
       temp_fail.On();
   }
   else
   {
       temp_fail.Off();
       temp_ok.On();
   }
   ...
}

当我使用-O3 优化进行编译时,它编译正常,但显然我无法调试我的程序(Eclipse 写“no source avaible for main() 0x....”。

要调试,我应该使用-O0 优化,但是当我尝试使用-O0 标志编译时,我会遇到如下错误:

对 `Pin::GPIOx 的未定义引用

使用谷歌我找到了this post。阅读后我明白了,我需要明确声明GPIOx 静态变量。

于是我开始在Pin class 中搜索GPIOx 静态变量,我发现了这个:

template<char port, int pin_no, char activestate>
struct Pin
{
    enum { GPIOx_BASE = port_gpio_t<port>::GPIOx_BASE };
    enum { IDR_BB_ADDR = PERIPH_BB_BASE + (GPIOx_BASE + offsetof(GPIO_TypeDef, IDR) - PERIPH_BASE) * 32 + pin_no * 4 };
    enum { ODR_BB_ADDR = PERIPH_BB_BASE + (GPIOx_BASE + offsetof(GPIO_TypeDef, ODR) - PERIPH_BASE) * 32 + pin_no * 4 };
    static struct
    {
        GPIO_TypeDef* operator-> () { return (GPIO_TypeDef*)GPIOx_BASE; }
    }GPIOx;
...
...other code

但我不明白我应该写什么代码来初始化未命名的结构?


编译器: arm-cortex-eabi-g++ v4.7.2

调试器: arm-none-eabi-gdb v7.10.1 + OpenOCD + JLink

IDE: Eclipse + CDT

操作系统: Linux Mint 17.3

【问题讨论】:

  • 我认为这个问题与 [stm32] 无关,而仅与 C++ 编译器/链接器问题有关。 STM32 控制器只是该类所针对的那种 ARM Cortex-M 控制器。但问题在于如何使用库,而不是控制器。

标签: c++ gdb compiler-optimization undefined-reference


【解决方案1】:

就在链接标题的顶部:

USAGE:
*
*   I. Declare pin typedef:
* typedef Pin<'A', 5, 'H'> PA5;    // PA5, active level = high
* typedef Pin<'A', 6> PA6;         // PA6, active level = high ('H' is default parameter)

所以,尝试这样声明:

typedef Pin<'C', 6>     temp_ok;
typedef Pin<'C', 7>     temp_fail;

【讨论】:

  • 这没有帮助:(
猜你喜欢
  • 2020-08-04
  • 2016-08-04
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多