27

I am using std::atomic with a custom class in my library. All works fine with MSVC, but now that i'm trying to get it to run on macOS, i get a linker error:

undefined symbols for architecture x86_64: "__atomic_store", referenced from: _main in main.o

I've created some test code to replicate this

#include <iostream>
#include <atomic>

using namespace std;

class Vec {
    public:
    int x, y, z;
    Vec() { x = y = z = 0; }
};

std::atomic<Vec> x;


int main()
{
  Vec a;
  x = a;
  cin.get();
    return 0;
}

Of course this example doesn't make much sense, but it's the shortest I could come up with. It does run in VS2012, but not in xcode (giving me the linker error shown above).

So what's going on? Am I abusing std::atomic here? The library I'm working on is heavily multithreaded and used for audio processing. So if I'm not using std::atomic in the correct way, it is not really showing, because performance is very good and I don't have any threading problems with it. Or is xcode perhaps lacking something?

Update:

I've checked andrey's answer because it has the most information, although all 3 answers are useful. I'm no expert in this (obviously) but it seems that VS2012 goes beyond what should be implemented in C++11.

So how to go from here? I see a few options.

  1. Don't use atomic for this class. In my particular case this would be very hard because my vector class is used all over the code. Locking and unlocking mutexes would probably slow things down a lot.
  2. Implement the functions myself for atomic. That looks very complicated to me. I'll save it as the last option.
  3. See if something can be done with boost::atomic. This seems to work at first glance. I have to do more tests on it though.
yvan vander sanden
  • 955
  • 1
  • 12
  • 13

2 Answers2

10

As described in http://en.cppreference.com/w/cpp/atomic/atomic:

The standard library provides full specializations of the std::atomic template for the following types:

1) One specialization for the type bool and its typedef
2) Specializations and typedefs for integral types
3) std::atomic for all pointer types

What about the Boost.Atomic. As described in Boost.Atomic limitations:

Using non-POD-classes as template parameter to atomic results in undefined behavior.

Andrey Volk
  • 3,513
  • 2
  • 17
  • 29
  • 5
    `std::atomic` only requires the type to be trivially copyable, and the class shown in the OP is trivially copyable. – bames53 Apr 08 '13 at 19:28
  • 4
    Note that "provides full specializations" does not imply that other types are not supported, only that they are not implemented through specializations. The general template `std::atomic` handles trivially copyable types. The specializations for numeric types provide additional member functions (such as arithmetic operations) that are not applicable to arbitrary user-defined types. – Pete Becker Apr 08 '13 at 20:04
  • But shouldn't that mean that the example should link if the constructor was left out? I'm still getting the same error if I comment it out. – yvan vander sanden Apr 08 '13 at 20:37
  • 1
    For gcc (since 4.7) you may try add -std=c++11 – Andrey Volk Apr 08 '13 at 20:57
  • Btw, the boost::atomic version gives me this linker error also. In this case a symbol not found for Boost::atomics::detail::lockpool::scoped_lock::scoped_lock(void const volatile*) – yvan vander sanden Apr 08 '13 at 21:02
  • Did you link with -lboost_system flag? – Andrey Volk Apr 08 '13 at 21:07
  • -lboost_system doesn't work. The linker complains that boost_system cannot be found. This is awkward because I already link with portaudio and libsndfile in this way, and they are located in the same directory (/usr/local/lib) – yvan vander sanden Apr 09 '13 at 14:01
0

Check which standard library you are using in project page in clang compiler settings. It should be GNU's libstdc++ with c++11 support or libc++.

Hope it'll help.

inkooboo
  • 2,904
  • 1
  • 19
  • 24
  • Thanks. It is good to check these settings. Although in this case this is not the problem, mainly because these settings seem to be standard in xcode 4.6 – yvan vander sanden Apr 08 '13 at 20:51
  • I checked your program on my mac and got the same error... Have no ideas now. Looks like a bug in standard library or not supported feature – inkooboo Apr 09 '13 at 07:05