I am having trouble building a C++ project that uses std::atomic
on Debian WSL.
I am unable to reproduce on Godbolt. Building the below code snippet
#include <atomic>
#include <cstdint>
// packed struct definition
#pragma pack(push, 1)
struct PropBoardSensorData {
PropBoardSensorData() = default;
uint32_t timestamp;
int32_t adc0, adc1, adc2, adc3, adc4, adc5, adc6, adc7, adc8, adc9, adc10, adc11, adc12, adc13, adc14;
int32_t loadCellRaw;
float tcInternalTemp;
float tcTemp1, tcTemp2, tcTemp3, tcTemp4;
// fault flags is a bitfield [fault, open, gnd, vcc] from LSB to MSB for each tc reader, 1 meaning that fault is active
uint8_t tcFaultFlags[4];
};
#pragma pack(pop)
int main(){
PropBoardSensorData data{};
data.adc10 = 5;
std::atomic<PropBoardSensorData> atomicData{data};
auto newData = atomicData.load();
std::atomic<PropBoardSensorData> defaultAtom{PropBoardSensorData{}};
auto newData2 = defaultAtom.load();
}
with a CMake like
cmake_minimum_required(VERSION 3.16)
project(atomic_lib_ex)
set(CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
add_compile_options(-latomic)
add_executable(main_test
main.cpp)
doesn't compile for some reason. It fails with
/usr/bin/ld: CMakeFiles/main_test.dir/main.cpp.o: in function `std::atomic<PropBoardSensorData>::load(std::memory_order) const':
main.cpp:(.text._ZNKSt6atomicI19PropBoardSensorDataE4loadESt12memory_order[_ZNKSt6atomicI19PropBoardSensorDataE4loadESt12memory_order]+0x4f): undefined reference to `__atomic_load'
collect2: error: ld returned 1 exit status
gmake[2]: *** [CMakeFiles/main_test.dir/build.make:97: main_test] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/main_test.dir/all] Error 2
gmake: *** [Makefile:91: all] Error 2
my versions are
$ g++ --version
g++ (Debian 12.2.0-14) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ ld --version
GNU ld (GNU Binutils for Debian) 2.40
Copyright (C) 2023 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
Even weirder, if I link the library directly, like
cmake_minimum_required(VERSION 3.16)
project(atomic_lib_ex)
set(CMAKE_CXX_STANDARD 17)
set (CMAKE_CXX_STANDARD_REQUIRED ON)
# add_compile_options(-latomic)
add_executable(main_test
main.cpp)
target_link_libraries(main_test PRIVATE
atomic)
then it builds fine.