How to fix linker error with atomic compare_exchange_weak
I have an atomic variable and want to use compare_exchange_weak on it.
But I'm getting the following linker error when compiling with STM32CubeIDE v1.8.0 (arm-none-eabi 9.3.1:(
arm-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'Here is a minimal example to demonstrate the linker error:
/* 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;
}Update 6.12.2021:
Platform:Windows version of CubeIDE 1.8.0
STM32 target: STM32F030CC
