c++ - 使用-O0优化对静态变量的 undefined reference [stm32模板引脚]

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

我在 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 写“main() 0x 没有可用的源......”。

要进行调试,我应该使用 -O0 优化,但是当我尝试使用 -O0 标志进行编译时,出现如下错误:

undefined reference to `Pin<(char)67, 11, (char)72>::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

最佳答案

在链接标题的顶部:

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;

关于c++ - 使用-O0优化对静态变量的 undefined reference [stm32模板引脚],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35769981/

相关文章:

c++ - 内联函数中的分支

c++ - 一个 C++ 函数,用于计算和采样射弹在 3D 空间中的轨迹。物理编程

c++ - 从函数返回 vector 给出 "vector subscript out of range"错误

c++ - 错误 C2784 : Could not deduce template argument

c++ - 在 gdb 的上层范围内检查类成员

c - GDB:如果变量值相等则中断

C++:如何使用尚未定义的类型?

gdb - 为 ESP32 和 QEMU 构建 GDB

c++ - -O3循环增量优化

clang:有没有办法指定 c11 原子操作中使用的低级指令?