How to compile code with #include <thread>

I am trying to compile some C ++ code that uses threads:

#include <iostream> #include <thread> void hello() { std::cout<<"Hello Concurrent World\n"; } int _main(int argc, _TCHAR* argv[]) { std::thread t(hello); t.join(); return 0; } 

I get compilation errors:

 c:\temp\app1\app1\app1.cpp(6): fatal error C1083: Cannot open include file: 'thread': No such file or directory ~/Documents/C++ $ g++ -o thread1 thread1.cpp -D_REENTRANT -lpthread In file included from /usr/include/c++/4.5/thread:35:0, from thread1.cpp:2: /usr/include/c++/4.5/bits/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options. 

How to fix these errors?

+6
source share
3 answers

<thread> , and standard thread support is a new feature (defined in the C ++ 11 standard). As for g ++, you should enable it by adding -std=c++0x to the command line, as described in the error message.

In addition, you use a non-standard (Microsoft-specific) main version, use the "classic" main and regular char :

 // thread1.cpp #include <iostream> #include <thread> void hello() { std::cout<<"Hello Concurrent World\n"; } int main(int argc, char * argv[]) { std::thread t(hello); t.join(); return 0; } 

Note that not all C ++ 11 features are available in current compilers; as for g ++, you can find the status of their implementation here .

+9
source

Yes, the header file <thread> is standard only in the latest C ++ 11 standard (completed only this year).

In GCC, you will need the latest version 4.6 , and even with it, not everything is supported by the C ++ 11 standard. See this table.

+3
source

As for MSVC, the C ++ 11 <thread> header is not supported in VS2010 - you will need to remove the Visual Studio 11 developer preview (http://msdn.microsoft.com/) en-us / vstudio / hh127353) to try him today.

For more on what's new in C ++ for Visual Studio 11, see http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx .

In addition, my PDF book file (which is currently in pre-release) has the following definition for main() :

 int main() { std::thread t(hello); t.join(); } 

which avoids the problems you are having with _TCHAR undefined in GCC.

+2
source

Source: https://habr.com/ru/post/903810/


All Articles