I've the following compile error when I try to use the method is_lock_free() of atomic.
struct Simple1 { int i; };
struct Simple2 { int a; int b; };
struct Simple3 { int a; int b; int c; };
int main()
{
atomic< Counter > counter;
atomic< Param > param;
atomic< Simple1 > s1;
atomic< Simple2 > s2;
atomic< Simple3 > s3;
cout
<< "Is s1 lock free? " << boolalpha << s1.is_lock_free() << '\n'
<< "Is s2 lock free? " << boolalpha << s2.is_lock_free() << '\n'
<< "Is s3 lock free? " << boolalpha << s3.is_lock_free() << '\n';
}
s1.is_lock_free() and s2.is_lock_free()
are okay and atomic< builtin >::is_lock_free()
, where builtin
is a fondamental type, works well.
But for s3 the call of is_lock_free
gives a linkage error: undefined reference to __atomic_is_lock_free
Does I need to link some external library? What can I do?
Edit 1
The same issue occur on gcc4.8.1 on Ubuntu 13.04
The comand line option is the following:
g++ -O0 -g3 -Wall -c -fmessage-length=0 -std=c++11 -o "atomic_test.o" "atomic_test.cpp"
g++ -std=c++11 -o hello.exe atomic_test.o
Edit 2
I added -latomic and -lpthread on gcc but I've the same error:
04:09:37: Running steps for project atomic...
04:09:37: Configuration unchanged, skipping qmake step.
04:09:37: Starting: "/usr/bin/make"
/home/dmdtek/Qt/5.1.0/gcc_64/bin/qmake -spec linux-g++ CONFIG+=debug CONFIG+=declarative_debug CONFIG+=qml_debug -o Makefile ../atomic/atomic.pro
g++ -latomic -lpthread -Wl,-rpath,/home/dmdtek/Qt/5.1.0/gcc_64 -o atomic main.o
main.o: In function `std::atomic<Simple3>::is_lock_free() const':
/usr/include/c++/4.8/atomic:191: undefined reference to `__atomic_is_lock_free'
main.o: In function `std::atomic<long double>::is_lock_free() const':
/usr/include/c++/4.8/atomic:191: undefined reference to `__atomic_is_lock_free'
collect2: error: ld returned 1 exit status
make: *** [atomic] Error 1
04:09:37: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project atomic (kit: Desktop Qt 5.1.0 GCC 64bit)
When executing step 'Make'
04:09:37: Elapsed time: 00:00.
libatomic is present on my system:
$ ldconfig -v | grep atomic
/sbin/ldconfig.real: Path `/lib/x86_64-linux-gnu' given more than once
/sbin/ldconfig.real: Path `/usr/lib/x86_64-linux-gnu' given more than once
libatomic.so.1 -> libatomic.so.1.0.0
/sbin/ldconfig.real: Can't create temporary cache file /etc/ld.so.cache~: Permission denied
You need to link with libatomic
(which is provided by GCC):
g++ -std=c++11 -o hello.exe atomic_test.o -latomic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With