有一个变量想对其使用 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;
}
|