-1

I know that the -latomic flag is required for the atomic library to be linked in by GCC. However, for some reason std::atomic<int> doesn't require it to build

build without latomic

while structs do

build requires latomic

what is this difference caused by?

k huang
  • 409
  • 3
  • 10
  • 1
    Please post the code and the errors in the question in form of a [mcve]. – 273K Aug 07 '23 at 19:00
  • @273K all of my examples are linked through Godbolt and are minimally reproducible. – k huang Aug 07 '23 at 19:01
  • A plausible explanation is that on your target architecture, atomic operations on int-sized operands are directly mapped onto processor instructions (via intrinsics) and don't need any library code. – ach Aug 07 '23 at 19:01
  • 1
    External links are not searchable on SO, the second code can be reduced to a few lines `#include struct PropBoardSensorData { float tcTemp1, tcTemp2, tcTemp3; }; int main(){ std::atomic atomicData{}; auto newData = atomicData.load(); }` – 273K Aug 07 '23 at 19:04
  • @273K "_External links are not searchable on SO_" Ironically, the external link does contain the code, so the posted message w/ just links is no shorter than one with full code. – curiousguy Aug 08 '23 at 04:21

1 Answers1

5

If the compiler inlines the __atomic builtins that std::atomic uses, the object file won't contain any references to functions that libatomic defines.

This is normal for most architectures with atomic<size_t> or smaller, at least with optimization enabled, and sometimes for structs the size of two pointers on some architectures. (But not x86-64 since GCC7, where it chooses not to inline lock cmpxchg16b even if you compile with -mcx16, and chooses not to report it as is_lock_free. https://gcc.gnu.org/ml/gcc-patches/2017-01/msg02344.html)

In your case, as you can see by mousing over the source and looking at the asm, auto newData = atomicData.load(); compiled to mov DWORD PTR [rbp-4], eax even in your debug built. (With a huge amount of clutter since you disabled optimization.)

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847