samedi 10 avril 2021

How to compile C++ application with #include

I have tried compiling my C++ File with #include <thread> using G++, but it failed. Here is my source code below:

#include <iostream>
#include <thread>
#include <windows.h>

using namespace std;

void act_1()
{
    cout << "Hello World from act_1\n";
    return;
}

int main()
{
        thread th(act_1);
        th.join();
        
        ExitProcess(EXIT_SUCCESS);
}

Now, I have used the flags -std=c++11 and -pthreads, but it is still not working. Here is the console output:

C:\Users\Mayukh\Desktop>g++ -std=c++11 -pthread threaded.cpp -o thread
threaded.cpp: In function 'int main()':
threaded.cpp:15:3: error: 'thread' was not declared in this scope
   thread th(act_1);
   ^~~~~~
threaded.cpp:15:3: note: 'std::thread' is defined in header '<thread>'; did you
forget to '#include <thread>'?
threaded.cpp:4:1:
+#include <thread>

threaded.cpp:15:3:
   thread th(act_1);
   ^~~~~~
threaded.cpp:16:3: error: 'th' was not declared in this scope
   th.join();
   ^~
threaded.cpp:16:3: note: suggested alternative: 'tm'
   th.join();
   ^~
   tm

Please help me

Aucun commentaire:

Enregistrer un commentaire