[STM32G0] 如何修复 compare_exchange_weak 的链接错误

[复制链接]
Hitomi22 发表于 2025-8-16 22:20 | 显示全部楼层 |阅读模式
有一个变量想对其使用 compare_exchange_weak。

但在使用 STM32CubeIDE v1.8.0 (arm-none-eabi 9.3.1) 编译时出现以下链接错误:

aarm-none-eabi\bin\ld.exe: ./Core/Src/main.o: in function `std::__atomic_base<unsigned char>::compare_exchange_weak(unsigned char&, unsigned char, std::memory_order, std::memory_order)':
arm-none-eabi\include\c++\9.3.1\bits/atomic_base.h:457: undefined reference to `__atomic_compare_exchange_1'
以下是一个演示链接错误的最小示例:
/* main.cpp */

#include <cstdint>
#include <cstdio>
#include <atomic>

int main() {
        std::atomic<uint8_t> some_atomic_var { 0 };

        some_atomic_var.store(3U);                   // atomic store works fine
        printf("%u\n", some_atomic_var.load());   // atomic load works fine

        auto val = some_atomic_var.load();

        // set second bit
        while (!some_atomic_var.compare_exchange_weak(val, val | (1 << 2U)));  // gives linker error
        printf("%u\n", some_atomic_var.load());

        return 0;
}

小明的同学 发表于 2025-8-17 10:28 | 显示全部楼层
weak这种函数是什么用处。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

43

主题

45

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部