gpt4 book ai didi

c++11 - mingw-4.8.1 原子问题

转载 作者:行者123 更新时间:2023-12-03 08:14:13 25 4
gpt4 key购买 nike

当我尝试使用 atomic 的 is_lock_free() 方法时,出现以下编译错误。

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()没事, atomic< builtin >::is_lock_free() ,其中 builtin是一种基础类型,效果很好。

但是对于 s3 调用 is_lock_free给出链接错误: undefined reference to __atomic_is_lock_free
我需要链接一些外部库吗?我能做什么?

编辑 1

同样的问题发生在 Ubuntu 13.04 上的 gcc4.8.1

命令行选项如下:
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

编辑 2

我在 gcc 上添加了 -latomic 和 -lpthread 但我有同样的错误:
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 存在于我的系统中:
$ 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

最佳答案

您需要与libatomic链接(由 GCC 提供):

g++ -std=c++11 -o hello.exe atomic_test.o -latomic

关于c++11 - mingw-4.8.1 原子问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17697232/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com