如何在 Linux 上的 C++ 中包含 <atomic>?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10787183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to include <atomic> in C++ on Linux?
提问by user1002288
I am trying to include atomic in my C++ pthread program.
我试图在我的 C++ pthread 程序中包含 atomic 。
#include <atomic>
But, i got error:
但是,我有错误:
error: atomic: No such file or directory
错误:原子:没有这样的文件或目录
I tried :
我试过 :
#include <asm/atomic>
#include <atomic.h>
#include <linux/atomic>
#include <util/atomic>
#include <stdcatomic>
#include <catomic>
No one works.
没有人工作。
My gcc is gcc version 4.1.2 20080704 (Red Hat 4.1.2-52)
我的 gcc 是 gcc version 4.1.2 20080704 (Red Hat 4.1.2-52)
The post :
帖子:
#include <cstdatomic> "no such file" in ubuntu
#include <cstdatomic> 在 ubuntu 中“没有这样的文件”
std::atomic support in g++ 4.4.3
do not work either.
也不行。
And #include <thread> also got : No such file or directory
Any help will be appreciated.
任何帮助将不胜感激。
thanks !
谢谢 !
UPDATE,
更新,
I am trying to install GCC 4.7 on Linux, but in "make check", I got error,
我正在尝试在 Linux 上安装 GCC 4.7,但在“make check”中,出现错误,
autogen -T /remote/mypath/gcc_4_7_2012_5_28/gcc_4_7_new_2012_5_29/trunk/fixincludes/check.tpl , remote/mypath/gcc_4_7_2012_5_28/gcc_4_7_new_2012_5_29/trunk/fixincludes/inclhack.def ,
make[2]: execvp: autogen: Permission denied, then I tried to install autogen, but
got: I need to install guile-devel, then when I installed guile-2.0.5-2.1.src.rpm ,
I got rpm -ivh guile-2.0.5-2.1.src.rpm, warning: guile-2.0.5-2.1.src.rpm: Header V3
RSA/SHA256 signature: NOKEY, key ID 3dbdc284
error: cannot write to %sourcedir /usr/src/redhat/SOURCES, I cannot get root
authorization.
Any help will be appreciated. thanks !
任何帮助将不胜感激。谢谢 !
采纳答案by Mahmoud Al-Qudsi
Your post is a little confusing, since you link to a post that says GCC 4.4 is required for atomic
, yet you are expecting it to work on 4.1.x.
您的帖子有点令人困惑,因为您链接到的帖子说 需要 GCC 4.4 atomic
,但您希望它可以在 4.1.x 上运行。
You'll need to upgrade to at least GCC 4.4 for atomic
support. There is no way to enable support for atomic in GCC 4.1, though you can use boost::atomic
instead. For future reference, this chartshows which version of GCC supports which C++11 features.
您至少需要升级到 GCC 4.4 以获得atomic
支持。尽管您可以使用,但无法在 GCC 4.1 中启用对原子的支持boost::atomic
。为了将来参考,此图表显示了哪个版本的 GCC 支持哪些 C++11 功能。
If for some reason you can't upgrade GCC, you could also try using a different compiler altogether (e.g. clang).
如果由于某种原因您无法升级 GCC,您也可以尝试完全使用不同的编译器(例如 clang)。
回答by pmr
This tablekeeps track of gcc implementation status. Your compiler version doesn't support atomics yet.
该表跟踪 gcc 的实现状态。您的编译器版本尚不支持原子。
In any case you need to use the std=c++11
(or c++0x
in older versions) switch to enable C++11 features.
在任何情况下,您都需要使用std=c++11
(或c++0x
在旧版本中)开关来启用 C++11 功能。
回答by jrouquie
Indeed gcc 4.4 could not compile for me a code containing #include <atomic>
.
This happened while compiling someone else's code (fasttext), so I prefered to avoid changing the code to #include <cstdatomic>
.
事实上,gcc 4.4 无法为我编译包含#include <atomic>
. 这发生在编译其他人的代码 ( fasttext) 时,所以我宁愿避免将代码更改为#include <cstdatomic>
.
I upgraded to gcc 4.8 and it worked.
我升级到 gcc 4.8 并且它起作用了。
If you're using CentOS 6.8, here is how to upgrade gcc.
如果您使用的是 CentOS 6.8,这里是升级 gcc 的方法。