Hitomi22 发表于 2025-8-16 22:20

如何修复 compare_exchange_weak 的链接错误

有一个变量想对其使用 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这种函数是什么用处。
页: [1]
查看完整版本: 如何修复 compare_exchange_weak 的链接错误