C++ – How to include in C++ on Linux

atomiccgcclinuxpthreads

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 !

Best Answer

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