- 1 year ago
How to compile C++ application with #include <thread> in windows (MinGW - W64) using G++?
To compile a C++ application with the #include <thread>
header on Windows using MinGW-w64 and g++, follow these steps:
Install MinGW-w64: Download and install MinGW-w64, a Windows development environment that includes the GCC compiler and related tools. Make sure to choose the appropriate version (32-bit or 64-bit) based on your system.
Set up MinGW-w64 in your system's PATH: Add the MinGW-w64 bin directory to your system's PATH environment variable. This allows you to access the g++ compiler from any location in the command prompt.
Create a C++ source file: Create a C++ source file, e.g.,
main.cpp
, and include the necessary code. For example:
cpp#include <iostream>
#include <thread>
void myThreadFunction() {
std::cout << "Hello from the thread!" << std::endl;
}
int main() {
std::thread myThread(myThreadFunction);
myThread.join();
return 0;
}
Compile the C++ application: Open a command prompt and navigate to the directory where the
main.cpp
file is located. Use the following command to compile the C++ application:For 32-bit:
shellg++ -std=c++11 -o myapp main.cpp
For 64-bit:
shellg++ -std=c++11 -m64 -o myapp main.cpp
This command instructs g++ to compile the source file with C++11 support (
-std=c++11
) and create an executable namedmyapp
.Run the compiled application: After successful compilation, you can run the application by executing the following command in the command prompt:
shellmyapp
If everything is set up correctly, you should see the output from the thread in the console.
By following these steps, you can compile a C++ application that includes the <thread>
header using MinGW-w64 and g++ on Windows. Adjust the compilation options and flags as needed based on your specific requirements and system configuration.