-1

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.

genpfault
  • 51,148
  • 11
  • 85
  • 139
k huang
  • 409
  • 3
  • 10
  • 2
    For what it's worth, I get the same compiler error "undefined reference to `__atomic_load`" with both g++ (13.2.1) and clang++ (15.0.7) on my (Arch Linux) system, with your code. Adding `-latomic` fixes it. – Jesper Juhl Aug 07 '23 at 19:08
  • `struct PropBoardSensorData` is way too big to be lock-free on any ISA. `std::atomic` is just going to use a spinlock around every access, a lot less efficient than doing your own locking around multiple accesses to the same object. – Peter Cordes Aug 07 '23 at 19:23
  • 2
    `add_compile_options` is the wrong stanza to use. (`-l...` is a *linker* flag after all) You meant `link_libraries(-latomic)`. But an explicit `target_link_libraries` is better. – Botje Aug 07 '23 at 19:54

0 Answers0