How to compile the code using #include

后端 未结 3 1287
渐次进展
渐次进展 2021-01-05 08:20

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

#include 
#include 
void hello()
{
    std::cout<<\"Hello Concurr         


        
相关标签:
3条回答
  • 2021-01-05 08:33

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

    With GCC, you'll need the very latest 4.6 version, and even with it, not everything is supported of the C++11 standard. See this table

    0 讨论(0)
  • 2021-01-05 08:35

    As far as MSVC goes, the C++11 <thread> header isn't supported in VS2010 - you'll need to pull down a Developer Preview of Visual Studio 11 (http://msdn.microsoft.com/en-us/vstudio/hh127353) to try it out today.

    See http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx for details on what is new in C++ for Visual Studio 11.

    Also, my PDF of the book (which is in pre-release at the moment) has the following definition of main():

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

    which avoids the problems you're running into with _TCHAR being undefined in GCC.

    0 讨论(0)
  • 2021-01-05 08:39

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

    Also, you are using a nonstandard (Microsoft-specific) main, use the "classic" main and normal 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;
    }
    

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

    0 讨论(0)
提交回复
热议问题