user1002288
user1002288

Reputation: 5040

How to include <atomic> in C++ on Linux?

I am trying to include atomic in my C++ pthread program.

#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)

The post :

#include <cstdatomic> "no such file" in 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,

   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 !

Upvotes: 3

Views: 30191

Answers (3)

jrouquie
jrouquie

Reputation: 4405

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>.

I upgraded to gcc 4.8 and it worked.

If you're using CentOS 6.8, here is how to upgrade gcc.

Upvotes: 1

Mahmoud Al-Qudsi
Mahmoud Al-Qudsi

Reputation: 29539

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.

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 chart shows which version of GCC supports which C++11 features.

If for some reason you can't upgrade GCC, you could also try using a different compiler altogether (e.g. clang).

Upvotes: 7

pmr
pmr

Reputation: 59811

This table keeps track of gcc implementation status. Your compiler version doesn't support atomics yet.

In any case you need to use the std=c++11 (or c++0x in older versions) switch to enable C++11 features.

Upvotes: 2

Related Questions