Simple thread example doesn't work in MinGW - c++

#include <iostream>
#include <thread>
using namespace std;
void thread_c() {
for (int i = 0; i < 11; ++i) {
cout << i;
}
}
int main() {
thread t(thread_c);
t.join();
return 0;
}
Such simple example, but it doesn't work on my g++ (MinGW.org GCC-6.3.0-1) 6.3.0, Windows 8.1
Here's the error:
main.cpp: In function 'int main()':
main.cpp:13:5: error: 'thread' was not declared in this scope
thread t(thread_c);
^~~~~~
main.cpp:14:5: error: 't' was not declared in this scope
t.join();
^
*I've been working on a big project, and noticed about some compilation errors. I wrote this code for testing my compiler out
What's wrong with it?

Loking at your code and the compiler results I see the following:
#include <thread> is not including a file that defines thread as your code expects
GCC 6.3.0 is very old (current is 10.2.0), and chances are it was built without POSIX thread support (and probably only support for Windows threads)
I always recommend using MinGW-w64 instead of MinGW as it is more up to date and supports both 32-bit and 64-bit Windows.
When I build your example with the MinGW-w64 from http://winlibs.com/ (which has POSIX thread support) I get no errors and the example works (output is 012345678910).

Related

Why does 'error: 'thread' is not a member of 'std' occur even after I've link thread in cmake? [duplicate]

I wrote this as a simplified version of a multithreading example to get a feel for it, but am running into some issues when compiling. My compiler says that thread is not a member of std and prompts me to add #include <thread> to my code even though I have already done so. I've been unable to find any similar problems so far, but I suspect that it is an issue with how I'm compiling it because my code is very similar to the example code.
#include <iostream>
#include <thread>
void doWork () {
std::cout << "Working...\n";
}
int main () {
std::thread worker(doWork);
work.join();
std::cout << "Finished\n";
return 0;
}
My compiler is MinGW g++ 9.2.0
I compiled with g++ main.cpp -o main and it gave me these errors:
main.cpp: In function 'int main()':
main.cpp:9:7: error: 'thread' is not a member of 'std'
9 | std::thread worker(doWork);
| ^~~~~~
main.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
2 | #include <thread>
+++ |+#include <thread>
3 |
main.cpp:11:2: error: 'work' was not declared in this scope
11 | work.join();
| ^~~~
MinGW-w64 by default comes with native (Win32) instead of POSIX threads support, and unfortunately there is currently no Win32 gthreads implementation (the threading subsystem of libstdc++), hence no threading functionality in GCC.
You need to switch from x86_64-w64-mingw32-g++-win32 to x86_64-w64-mingw32-g++-posix package to get std::thread working in MinGW-w64.
The question mingw-w64 threads: posix vs win32 discusses this issue in more detail.

Can't run to_string() function on Linux

I've got some code that I'm running on Mac OS X that can't be compiled on the Virtual Machine running Linux Mint. This is a simple example. When I run it in Mac, all is fine, but I'm getting issues when I run the same code on Linux, so I'm assuming the library I'm including is not there, but should I be getting an include error then?
Here's the example code that runs on Mac.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstdlib>
using namespace std;
int main(){
for (int i = 0; i < 10; i++){
string test = to_string(i);
cout << test << endl;
}
cout << "done" << endl;
return 0;
}
I get no issues here but running on Linux Mint, I get this when I try to compile:
for.cpp: In function 'int main()':
for.cpp:7:28 error: 'to_string' was not declared in this scope
string test = to_string(i);
^
make: *** [for] Error 1
Am I missing something? Any help would be much appreciated!
edit
I realize I forgot to include <string> on here and I fixed it, but what I changed (<string> included) still doesn't compile on Linux. I've used to_string before. I know that much in C++. I also tried adding <cstdlib>. Once again, this DOES compile on Mac and DOES NOT compile on Linux.
Here is my OSX output:
0
1
2
3
4
5
6
7
8
9
done
Here is my output on Linux Mint (Once again, Virtual Box, g++ make):
test.cpp: In function ‘int main()’:
test.cpp:9:28: error: ‘to_string’ was not declared in this scope
string test = to_string(i);
^
make: *** [test] Error 1
You could reproduce the problem yourself if you don't believe me. It's the same code, you can see for yourself if you want.
Compile your for.cpp file like this:
g++ -std=c++11 for.cpp
and run it with:
./a.out
The support for the to_string function in the <string> header was added in the C++11 version of the language, so you need to tell GCC to use that version. You can use the c++0x flag too, for example:
g++ -std=c++0x for.cpp
And you don't have to worry about <cstdlib>, that has nothing to do with it...
to_string() is defined in <string> if you are compiling with C++11 (but is not defined, or unreliably defined as an extension feature, if you are compiling with an earlier version of C++).
Reference: http://en.cppreference.com/w/cpp/string/basic_string/to_string
SOLUTION:
I found a better solution. For some reason, I've read stdlib.h will not work on some linux systems. I used a different function to convert int to string.
on linux:
#include <stdio.h>
and then
for (int i = 0; i < 10; i++){
char buffer[10];
sprintf(buffer,"%d",i);
string stringInt = buffer;
cout << stringInt << endl;
// do whatever you want with the string
}
edit
To the person that down voted my solution to this, here's a post from six years ago basically saying the same thing.

Weird thread issue

Alright im compiling the following simple chunk of code (found on cplusplus.com) on CodeBlocks IDE 12.11 with MinGW (downloaded separately and latest version too as of today).
The thing is that it shows the following errors upon compilation:
12: error: 'thread' was not declared in this scope
12: error: expected ';' before 't1'
13: error: 't1' was not declared in this scope
#include <iostream>
#include <thread>
using namespace std;
void hello(void){
cout << "hey there!" << endl;
}
int main()
{
thread t1(hello);
t1.join();
return 0;
}
Are threads not supported by GCC completely?Do i need to add flags to my compiler, and how do i do it on a codeblocks project? thanks in advance
Add --std=c++11 -pthread to your Compiler Flags

Why are std::stoi and std::array not compiling with g++ c++11?

I've been learning C++ and using the Terminal for the last couple of months. My code was compiling and running fine using g++ and C++11, but in the last couple of days it started giving errors and I have had problems compiling since. The only programs I can compile and run depend on older C++ standards.
The errors I first got related to #include < array > in the header file. Not sure why this happened, but I got around it by using boost/array instead. Another error I can't solve is with std::stoi. Both array and stoi should be in the C++11 standard library. I made the following simple code to demonstrate what's going on:
//
// stoi_test.cpp
//
// Created by ecg
//
#include <iostream>
#include <string> // stoi should be in here
int main() {
std::string test = "12345";
int myint = std::stoi(test); // using stoi, specifying in standard library
std::cout << myint << '\n'; // printing the integer
return(0);
}
Try to compile using ecg$ g++ -o stoi_trial stoi_trial.cpp -std=c++11
array.cpp:13:22: error: no member named 'stoi' in namespace 'std'; did you mean
'atoi'?
int myint = std::stoi(test);
~~~~~^~~~
atoi
/usr/include/stdlib.h:149:6: note: 'atoi' declared here
int atoi(const char *);
^
array.cpp:13:27: error: no viable conversion from 'std::string' (aka
'basic_string') to 'const char *'
int myint = std::stoi(test);
^~~~
/usr/include/stdlib.h:149:23: note: passing argument to parameter here
int atoi(const char *);
^
2 errors generated.
I also get these errors at compilation when using gcc or clang++ and with -std=gnu++11 (I guess they all depend on the same file structure). I also get the same error whether I specify std:: in the code, or if I specify using namespace std;
I worry that these issues arose because of the September Command Line Tools update via Xcode or because I installed boost and this somehow messed up my C++11 libraries. Hopefully there is a simple solution.
My system:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-> dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.76) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
Thanks for any insight you can offer.
clang has a weird stdlib, you need to add the following flag when you compile
-stdlib=libc++
your snippet works on my mac with
g++ -std=gnu++11 -stdlib=libc++ test.cpp -o test
This answer describes the problem

Error when compiling gcc 4.6.1 C++0x threading code on MacOSX Lion

When compiling the following code:
#include <iostream>
#include <thread>
using namespace std;
void hello()
{
cout << "Hello World!" << endl;
}
int main()
{
cout << "starting" << endl;
thread t(hello);
t.join();
cout << "ending" << endl;
return 0;
}
using:
$ g++-4.6.1 -std=c++0x -pthread threading.cpp
I get the following error:
threading.cc: In function ‘int main()’:
threading.cc:13:2: error: ‘thread’ was not declared in this scope
threading.cc:13:9: error: expected ‘;’ before ‘t’
threading.cc:14:2: error: ‘t’ was not declared in this scope
This is on MacOSX Lion with a custom built gcc 4.6.1. All the other c++0x features that are valid for gcc 4.6 works like a charm. Is this a MacOSX specific error?
std::thread (and the rest of the C++11 thread library) is only available for some of the platforms supported by gcc 4.6.1. Unfortunately for you, MacOSX is not one of those platforms.
My commercial Just::Thread library provides the C++11 thread facilities for 32-bit MacOSX with gcc 4.5, but gcc 4.6 is not supported yet.
See http://gcc.gnu.org/PR50196 - Mac OS X doesn't support some parts of pthreads that we rely on. Building the latest version won't help, but it might be fixed for GCC 4.7